ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

使用curl的php中的OAuth 2.0

2019-10-29 07:29:54  阅读:300  来源: 互联网

标签:curl oauth-2-0 google-api http-post php


我需要获取OAuth 2.0的access_token和refresh_token才能访问Google API,以下php脚本应返回一个带有access_token,refresh_token的json,如下所示:

{
  "access_token" : "####",
  "token_type" : "Bearer",
  "expires_in" : 3600,
  "refresh_token" : "####"
}

但是,PHP脚本仅向我返回此错误消息:

{
"error" : "invalid_request",
"error_description" : "Client must specify either client_id or client_assertion, not both"
}

我试图删除client_secret / client_id并仅使用client_id / client_secret,但仍然遇到相同的错误.

PHP脚本

$client_id = '###.apps.googleusercontent.com';
$redirect_uri = 'http://localhost/phpConnectToDB/csv/refreshFusionTable.php';
$client_secret = '###';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token");

$code = $_REQUEST['code'];

curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);


curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'code' => $code,
'client_id' => $clientID,
'client_secret' => $clientSecret,
'redirect_uri' => $redirect_uri,
'grant_type' => 'authorization_code'
));

$data = curl_exec($ch);

var_dump($data);

虽然cmd中的curl可以正常工作,并且可以返回我访问和刷新令牌,但没有任何错误.

curl --data "code=###&client_id=###.apps.googleusercontent.com&client_secret=###&redirect_uri=http://localhost/phpConnectToDB/csv/refreshFusionTable.php&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token

尽管.php脚本存在并且位于给定的路径上,但我不明白为什么会收到丢失的方案错误.请问你能帮帮我吗 ?

EDIT问题,“ redirect_uri的参数值无效:缺少方案”已解决,我只替换了’redirect_uri’=> urlencode($redirect_uri),带有此“ redirect_uri” => $redirect_uri,在CURLOPT_POSTFIELDS中.

解决方法:

哇,愚蠢的错误,我应该休息一下.

变量名称不匹配.
我定义了:

$client_id = '###.apps.googleusercontent.com';
$client_secret = '###';

但是这里我使用了不存在的clientID clientSecret

curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'code' => $code,
'client_id' => $clientID,
'client_secret' => $clientSecret,
'redirect_uri' => $redirect_uri,
'grant_type' => 'authorization_code'
));

固定且有效的PHP脚本

$client_id = '###.apps.googleusercontent.com';
$redirect_uri = 'http://localhost/phpConnectToDB/csv/refreshFusionTable.php';
$client_secret = '###';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token");

curl_setopt($ch, CURLOPT_POST, TRUE);

$code = $_REQUEST['code'];

// This option is set to TRUE so that the response
// doesnot get printed and is stored directly in
// the variable
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'code' => $code,
'client_id' => $client_id,
'client_secret' => $client_secret,
'redirect_uri' => $redirect_uri,
'grant_type' => 'authorization_code'
));

$data = curl_exec($ch);

var_dump($data);

但是我不得不说,谷歌在这里提供了一些误导性的错误消息,因为我没有定义client_id和client_secret,错误消息是:

{
"error" : "invalid_request",
"error_description" : "Client must specify either client_id or client_assertion, not both"
}

标签:curl,oauth-2-0,google-api,http-post,php
来源: https://codeday.me/bug/20191029/1958359.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有