如何通过授权码获取PowerBI访问令牌?

时间:2017-03-28 00:57:15

标签: php powerbi

我设法通过php curl获取授权代码,现在我开始陷入获取访问令牌的困境了。我收到错误消息:“error”:“unauthorized_client”,“error_description”:此API版本不支持“AADSTS70001:应用程序'02d8ad58-3e3b-4873-97ff-xxxxxxxxxxx'。

下面是代码:

public function getAccessToken() {

$code = $_GET['code'];

Log::info($code);

$curl = curl_init();

$fields = array(  
    'grant_type' => 'authorization_code', 
    'client_id' => 'my client id', 
    'client_secret' => 'my secrect', 
    'code' => $code, 
    'redirect_uri' => 'https://localhost/gettoken'

);

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://login.microsoftonline.com/common/oauth2/v2.0/token",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_HTTPHEADER => array(
    "content-type: application/x-www-form-urlencoded"
  ),
));

curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($fields));

$response = curl_exec($curl);

$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Log::info($response);

return redirect('report');

}

1 个答案:

答案 0 :(得分:0)

您可以通过点击此链接https://community.powerbi.com/t5/Developer/How-To-Get-embed-token-using-Get-Post-only/td-p/294475直接生成访问令牌。这是代码段,可帮助您生成访问令牌。

$postUrl="https://login.microsoftonline.com/common/oauth2/token";
$post_params = array(
    'grant_type' => 'password',
    'scope' => 'openid',
    'resource' => '<resourse>',
    'client_id' => '<client-id>',
    'username' => '<username>',
    'password' => '<password>'                
);
$ch = curl_init( $postUrl );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_params);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
echo $response;