Forum Discussion

insoluzioni's avatar
insoluzioni
Icon for Advocate I rankAdvocate I
9 years ago
Solved

Expiration information about AccessToken generated with PowerBI Rest API

Hello, I am trying to automatically import a PBIX file into my workspace with Power BI Embedded, currently using the code below, but I get the error:  HTTP Error 400. The request is badly formed. ...
  • insoluzioni's avatar
    insoluzioni
    9 years ago

    Thanks for the reply Eric_Zhang!

    Actually, the problem was in the DisplayName variable, which had spaces. Solved that by removing these spaces, but don't know if there's another workaround.

     

    I've worked hard to make this work in PHP, but happened to be some little details in the syntax of the HTTP request. I'm sharing my solution below to successfuly import a PBIX file to your workspace:

     

     

    /**
     * Imports specified PBIX file to workspace collection
     *
     * @param string $file_path
     * @param string $file_name
     * @param string $display_name
     * @return void
     */
    
    function importPBIX($file_path, $file_name, $display_name) {
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_URL, "https://api.powerbi.com/v1.0/collections/".COLLECTION_NAME.
                "/workspaces/".WORKSPACE_ID."/imports?datasetDisplayName=$display_name");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
        curl_setopt($ch, CURLOPT_POST, TRUE);
        $boundary = "BOUNDARY";
        $postdata = "--".$boundary . "\r\n";
        $postdata .= "Content-Disposition: form-data; name=\"" . $file_name . "\"; filename=\"" . $file_name . "\"\r\n";
        $postdata .= "Content-Type: application/octet-stream\r\n\r\n";
        $postdata .= file_get_contents($file_path);
        $postdata .= "\r\n";
        $postdata .= "--".$boundary . "--\r\n";
    
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            "Content-Type: multipart/form-data; boundary=\"$boundary\"",
            "Authorization: AppKey " . APP_KEY
        )); 
        curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false );
    
        $response = curl_exec($ch);
        curl_close($ch);
    
        // Retorna o id do relatório importado
        $responseJson = json_decode($response, TRUE);
        $repoId = $responseJson['id'];
    
        // Retorna dados do relatório   
        return $repoId;
    }

    Pay attention to the constants COLLECTION_NAME, WORKSPACE_ID AND APP_KEY that must be defined previously in this code.

    Thanks!