[英]Google API curl to get refresh token in php
I am using php and curl to retrieve review data and want to automate the process of getting access tokens using a refresh token.我正在使用 php 和 curl 来检索评论数据,并希望使用刷新令牌自动化获取访问令牌的过程。 I understand how to do this using http/rest as documented here:
我了解如何使用 http/rest 来做到这一点,如此处所述:
https://developers.google.com/identity/protocols/oauth2/web-server https://developers.google.com/identity/protocols/oauth2/web-server
I am trying to follow the "Refreshing an access token (offline access)" section and I know I need to do a POST request but am not sure how to do this with curl in php.我正在尝试遵循“刷新访问令牌(离线访问)”部分,我知道我需要执行 POST 请求,但不确定如何在 php 中使用 curl 执行此操作。
Here is what I have now:这是我现在所拥有的:
function getToken($token_url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$result = curl_exec($ch);
}
$token_url = "https://oauth2.googleapis.com/token" . "&client_id=" . $client_id . "&client_secret=" . $client_secret . "&refresh_token=" . $refresh_token . "&grant_type=" . $grant_type;
getToken($token_url);
So looking at the documentation provided, to perform the HTTP/REST request in PHP with the CURL module the code would look something like this:因此,查看提供的文档,要使用 CURL 模块在 PHP 中执行 HTTP/REST 请求,代码将如下所示:
function getToken($token_url, $request_data) {
$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request_data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/x-www-form-urlencoded"
));
$result = curl_exec($ch);
}
$token_url = "https://oauth2.googleapis.com/token";
$request_data = array(
"client_id" => $client_id,
"client_secret" => $client_secret,
"refresh_token" => $refresh_token,
"grant_type" => "refresh_token" // Constant for this request
);
getToken($token_url, $request_data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.