如何通过php和curl正确获取onedrive访问令牌

我正在尝试获取 onedrive 访问令牌。 这是请求:


GET https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id={client_id}&scope={scope}

    &response_type=token&redirect_uri={redirect_uri}

这是回应:


https://myapp.com/auth-redirect#access_token=EwC...EB

  &authentication_token=eyJ...3EM&token_type=bearer&expires_in=3600

  &scope=onedrive.readwrite&user_id=3626...1d

我已经在其他中实现了下面的两个代码来获取访问令牌,但是当页面被重定向以进行登录时出现以下错误。


AADSTS900144:请求正文必须包含以下参数:“client_id”。


范围属性以空格分隔,因此请参阅 onedrive 文档链接


代码 1


$data_string = array(

"client_id" => "myclient id",

"response_type" => "token",

"redirect_uri" => "http://localhost/test/callback.php"

"scope" => ['files.readwrite', 'offline_access'],

); 

$data = json_encode($data_string); 

$curl = curl_init();


curl_setopt_array($curl, array( 

CURLOPT_URL => "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?", 

CURLOPT_RETURNTRANSFER => true, 

CURLOPT_ENCODING => "", 

CURLOPT_MAXREDIRS => 10, 

CURLOPT_TIMEOUT => 30, 

CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 

CURLOPT_CUSTOMREQUEST => "GET", 

CURLOPT_POSTFIELDS => "$data", 

));


$result = curl_exec($curl); 

$err = curl_error($curl);


curl_close($curl);

代码 2


$curl = curl_init();

curl_setopt_array($curl, array( 

CURLOPT_URL => "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=myclientid-goes-here&scope=files.readwrite.all offline_access

    &response_type=token&redirect_uri=http://localhost/test/callback.php", 

CURLOPT_RETURNTRANSFER => true, 

CURLOPT_ENCODING => "", 

CURLOPT_MAXREDIRS => 10, 

CURLOPT_TIMEOUT => 30, 

CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, 

CURLOPT_CUSTOMREQUEST => "GET", 

//CURLOPT_POSTFIELDS => "$data", 

));


$result = curl_exec($curl); 

$err = curl_error($curl);


curl_close($curl);


哈士奇WWW
浏览 245回答 1
1回答

繁华开满天机

这是使用http_build_query()函数解决的,因此$data = http_build_query($data_string, '', '&');
打开App,查看更多内容
随时随地看视频慕课网APP