Google API curl 以在 php 中获取刷新令牌
Google API curl to get refresh token in php
我正在使用 php 和 curl 来检索评论数据,并希望使用刷新令牌自动执行获取访问令牌的过程。我了解如何使用 http/rest 执行此操作,如此处所述:
https://developers.google.com/identity/protocols/oauth2/web-server
我正在尝试按照“刷新访问令牌(离线访问)”部分进行操作,我知道我需要执行 POST 请求,但不确定如何在 [=24 中使用 curl 执行此操作=].
这是我现在拥有的:
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);
因此查看提供的文档,要使用 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);
我正在使用 php 和 curl 来检索评论数据,并希望使用刷新令牌自动执行获取访问令牌的过程。我了解如何使用 http/rest 执行此操作,如此处所述:
https://developers.google.com/identity/protocols/oauth2/web-server
我正在尝试按照“刷新访问令牌(离线访问)”部分进行操作,我知道我需要执行 POST 请求,但不确定如何在 [=24 中使用 curl 执行此操作=].
这是我现在拥有的:
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);
因此查看提供的文档,要使用 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);