使用CURL获取Google Client API访问令牌 [英] Get Google Client API access token using CURL

查看:321
本文介绍了使用CURL获取Google Client API访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在过去几天一直在尝试使用Google Oauth2获取访问令牌和刷新令牌,但无法到达任何地方。



文档是垃圾和我找不到任何工作的示例。



我刚开始使用CURL尝试获取访问代码和刷新令牌,但我不断收到错误响应。



我已尝试使用此

  // init the resource 
$ url ='https://accounts.google.com/o/oauth2/token';
$ ch = curl_init($ url);

curl_setopt($ ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ ch,CURLOPT_FAILONERROR,false);
curl_setopt($ ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ ch,CURLOPT_CUSTOMREQUEST,'POST');

curl_setopt($ ch,CURLOPT_HTTPHEADER,array(
'Content-Type:application / x-www-form-urlencoded',
'Content-length:0'
));

curl_setopt($ ch,CURLOPT_POSTFIELDS,
'code ='。urlencode('AUTHORISATION_CODE')。'&'。
'client_id ='。urlencode('MY_CLINET_ID。 apps.googleusercontent.com')。'&'。
'client_secret ='。urlencode('MY_CLIENT_SECRET')。'&'。
'redirect_uri = http://www.example.com /'。'&'。
'grant_type = authorization_code'
);

//执行
$ output = curl_exec($ ch);

echo $ output;

// free
curl_close($ ch);

但我只是得到这个回应

  {
error:invalid_request,
error_description:缺少必填参数:grant_type
}



我已经尝试从命令行运行CURL

  curl --datacode = AUTHORISATION_CODE& client_id = MY_CLIENT_ID.apps.googleusercontent.com& client_secret = MY_CLIENT_SECRET& redirect_uri = http://www.example.com/& grant_type = authorization_codehttps: //accounts.google.com/o/oauth2/token 

但我只是得到不同的回应

  {
error:invalid_client,
error_description:发现。
}

为什么获取访问令牌很困难?



UPDATE



我不知道发生了什么,但我不再收到我获得了访问令牌,但没有刷新令牌



我看过这篇文章未收到Google OAuth刷新令牌,并且我已撤销对我的应用的访问权以重新生成刷新令牌,但使用此代码会显示错误关于无效的代码

解决方案

我现在已经解决了使用Curl的问题。似乎没有任何理由为什么上述没有工作作为只是保持尝试,它最终在撤消访问应用程序,授权应用程序,获取代码和使用Curl请求中的代码后工作。



这解决了我上一个问题 YouTube API v3每次都会要求授权


I have been trying for the last couple of days trying to get an access token and refresh token using Google Oauth2 but not getting anywhere.

The documentation is rubbish and I can't find any examples that work.

I have just started to use CURL trying to get the access code and refresh token but I keep getting errors with the response.

I have tried using this

// init the resource
$url = 'https://accounts.google.com/o/oauth2/token';
$ch = curl_init($url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($ch, CURLOPT_FAILONERROR, false);  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/x-www-form-urlencoded',
    'Content-length: 0'
));

curl_setopt($ch, CURLOPT_POSTFIELDS,
    'code=' . urlencode('AUTHORISATION_CODE') . '&' .
    'client_id=' . urlencode('MY_CLINET_ID.apps.googleusercontent.com') . '&' .
    'client_secret=' . urlencode('MY_CLIENT_SECRET') . '&' .
    'redirect_uri=http://www.example.com/' . '&' .
    'grant_type=authorization_code'
); 

// execute
$output = curl_exec($ch);

echo $output;

// free
curl_close($ch);

but i just get this response

{ 
   "error" : "invalid_request", 
   "error_description" : "Required parameter is missing: grant_type"
}

and I've tried running CURL from the command line

curl --data "code=AUTHORISATION_CODE&client_id=MY_CLIENT_ID.apps.googleusercontent.com&client_secret=MY_CLIENT_SECRET&redirect_uri=http://www.example.com/&grant_type=authorization_code" https://accounts.google.com/o/oauth2/token

but i just get a different response

{
  "error" : "invalid_client",
  "error_description" : "The OAuth client was not found."
}

Why is it so difficult just to get an access token?

UPDATE

I don't know what's happening but I don't receive that error anymore and I get an access token but not a refresh token

I've looked at this post Google OAuth refresh token is not being received and I've revoked access to my app to regenerate the refresh token but using this code displays an error about an invalid code

解决方案

I have now fixed the problem using Curl. Didn't seem any reason why the above didn't work as just keeping trying, it worked eventually after revoking access to app, authing the app, getting the code and using the code in the Curl request.

This solved my previous question YouTube API v3 keeps asking for authorisation every time

这篇关于使用CURL获取Google Client API访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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