gpt4 book ai didi

PHP。如何获取访问 token 。使用 Google API 的 OAuth 2.0 进行身份验证

转载 作者:行者123 更新时间:2023-12-03 02:37:40 28 4
gpt4 key购买 nike

I found this function据说可以获取 accessToken,但我什么也没得到。我确实有 $_REQUEST['code'] 以及该函数所需的其他信息。

你知道这里出了什么问题吗?谢谢。

//Oauth 2.0: exchange token for session token so multiple calls can be made to api
if(isset($_REQUEST['code'])){
$_SESSION['accessToken'] = get_oauth2_token($_REQUEST['code']);
}

//returns session token for calls to API using oauth 2.0
function get_oauth2_token($code) {
global $client_id;
global $client_secret;
global $redirect_uri;

$oauth2token_url = "https://accounts.google.com/o/oauth2/token";
$clienttoken_post = array(
"code" => $code,
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => $redirect_uri,
"grant_type" => "authorization_code"
);

$curl = curl_init($oauth2token_url);

curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

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

$authObj = json_decode($json_response);

if (isset($authObj->refresh_token)){
//refresh token only granted on first authorization for offline access
//save to db for future use (db saving not included in example)
global $refreshToken;
$refreshToken = $authObj->refresh_token;
}

$accessToken = $authObj->access_token;
return $accessToken;
}

最佳答案

这就是我为获取访问 token 和刷新 token 所做的事情。

创建一个包含以下代码的文件:

<?php

if (isset($_GET['code'])) {
// try to get an access token
$code = $_GET['code'];
$url = 'https://accounts.google.com/o/oauth2/token';
$params = array(
"code" => $code,
"client_id" => YOUR_CLIENT_ID,
"client_secret" => YOUR_CLIENT_SECRET,
"redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
"grant_type" => "authorization_code"
);

$ch = curl_init();
curl_setopt($ch, constant("CURLOPT_" . 'URL'), $url);
curl_setopt($ch, constant("CURLOPT_" . 'POST'), true);
curl_setopt($ch, constant("CURLOPT_" . 'POSTFIELDS'), $params);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
if ($info['http_code'] === 200) {
header('Content-Type: ' . $info['content_type']);
return $output;
} else {
return 'An error happened';
}
} else {

$url = "https://accounts.google.com/o/oauth2/auth";

$params = array(
"response_type" => "code",
"client_id" => YOUR_CLIENT_ID,
"redirect_uri" => 'http://' . $_SERVER["HTTP_HOST"] . $_SERVER["PHP_SELF"],
"scope" => "https://www.googleapis.com/auth/plus.me"
);

$request_to = $url . '?' . http_build_query($params);

header("Location: " . $request_to);
}

现在,将 YOUR_CLIENT_IDYOUR_CLIENT_SECRET 替换为您的客户端 ID 和客户端 key 。

确保您的范围正确。例如,如果您想访问 Analytics,则应为 https://www.googleapis.com/auth/analytics

如果运行该文件,您应该会看到 OAuth2 批准屏幕。

如果您现在按接受,您应该得到如下所示的结果:

{
"access_token" : YOUR_ACCESS_TOKEN,
"token_type" : "Bearer",
"expires_in" : 3600,
"refresh_token" : YOUR_REFRESH_TOKEN
}

结果可能包含其他字段,具体取决于您申请的范围。

关于PHP。如何获取访问 token 。使用 Google API 的 OAuth 2.0 进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8902376/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com