简体   繁体   English

使用PHP curl的Gmail API访问令牌

[英]Gmail API access token with PHP curl

Facing issue while retrieving token for API call. 在检索API调用的令牌时遇到问题。 Here is my code 这是我的代码

$authUrl = "https://accounts.google.com/o/oauth2/token";

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $authUrl);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
    'code' => $code,
    'client_id' => CLIENT_ID,
    'client_secret' => CLIENT_SECRET,
    'redirect_uri' => $redirectURL,
    'grant_type' => 'authorization_code'
));

$http_data = curl_exec($curl);  
curl_close($curl);

var_dump($http_data);

$http_data is saying invalid request. $ http_data表示无效请求。

My task is to get all emails in my gmail account and I think for that I need access token. 我的任务是在我的Gmail帐户中收到所有电子邮件,我认为我需要访问令牌。 Please advise. 请指教。

Thanx in Advance. Thanx提前。

First you appear to be adding all of the parameters as part of the HTTP GET string this is a HTTP POST call. 首先,您似乎将所有参数添加为HTTP GET字符串的一部分,这是HTTP POST调用。 Also you appear to have forgotten to include the code you got from the first part of the auth flow. 此外,您似乎忘记了包含从auth流程的第一部分获得的代码

That is the Authentication Code, it is used to request a refresh token. 这是验证码,它用于请求刷新令牌。 It is displayed to the user in the body of the html as well as in the title of the page. 它在html的正文和页面标题中显示给用户。 To get a Refresh Token you POST the Authentication code back to Google. 要获取刷新令牌,请将身份验证代码发回Google。 Note: This is a HTTP Post you cant just place it in a browser that would be a HTTP Get. 注意:这是一个HTTP Post,你不能将它放在一个HTTP Get的浏览器中。 Note: grant_type=authorization_code 注意:grant_type = authorization_code

https://accounts.google.com/o/oauth2/token
code={Your code from the first part of flow}&client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code

Note have you considered using the Google APIs PHP Client library which will handle all of this for you? 注意您是否考虑过使用Google API PHP客户端库来处理所有这些问题?

Code ripped from my Google 3 legged Oauth2 flow article. 从我的谷歌3腿Oauth2流文章中删除了代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM