OAuth 2.0在php中使用curl [英] OAuth 2.0 in php using curl

查看:1043
本文介绍了OAuth 2.0在php中使用curl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to get my access_token and refresh_token for OAuth 2.0 to Access Google APIs, the php script below should return a json with access_token, refresh_token like this:

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

但是,php脚本返回我只有此错误消息:

but, the php script return me only this error message:

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

删除client_secret / client_id并且只使用client_id / client_secret,但仍然会收到相同的错误。

I tried to remove client_secret/client_id and use only client_id/client_secret, but still get the same error.

$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);

虽然curl在cmd工作,并返回我访问和刷新令牌没有任何错误。

Although curl in cmd works and returns me access and refresh token without any errors.

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脚本存在,它位于给定的路径。

I don't understand why I get the missing scheme error, although the .php script exists and it's located on the given path. Could you help me please ?

EDIT

问题redirect_uri:Missing scheme的无效参数值解决了,我刚刚替换了'redirect_uri'= >

EDIT

Problem with "Invalid parameter value for redirect_uri: Missing scheme" solved, I just replaced 'redirect_uri' => urlencode($redirect_uri), with this 'redirect_uri' => $redirect_uri, in CURLOPT_POSTFIELDS.

推荐答案

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

Wow, stupid misstake, I should have a rest.

变量名称不匹配。
我定义:

The variables names doesn't match. I defined:

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

但这里我使用了一个不存在的clientID,clientSecret

But here I used an non-existing 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脚本



Fixed and working PHP script

$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);

但我不得不说,google提供了一个误导性的错误信息,因为我没有既未定义client_id也未定义client_secret和错误消息:

But I have to say, that google provides a little misleading error message here, because I haven't defined neither client_id nor client_secret and error message was:

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

这篇关于OAuth 2.0在php中使用curl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆