Ben*_*all 4 google-api google-api-client google-oauth google-api-php-client
我正在使用 google-api-php-client 从客户端 0auth 流请求访问令牌,但它返回错误“无法从请求确定客户端 ID”。
我的代码如下。
我的 client_secret.json:
"web": {
"client_id":"123",
"project_id":"1234",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"123456789",
"redirect_uris":[
"http://localhost:7777/v1/oauth2callback"
],
"javascript_origins":[
"http://localhost:7777"
]
}
}
Run Code Online (Sandbox Code Playgroud)
我的身份验证 URL 创建:
$client->setAuthConfig(base_path() . '/client_secret.json');
$client->addScope(\Google_Service_Calendar::CALENDAR);
$client->setRedirectUri(URL::to('/') . '/v1/oauth2callback');
$client->setAccessType('offline');
$client->setPrompt('consent');
$client->setIncludeGrantedScopes(true);
$url = $client->createAuthUrl();
print filter_var($url, FILTER_SANITIZE_URL);
Run Code Online (Sandbox Code Playgroud)
然后我手动转到 URL:
身份验证流程似乎一切正常,并将我返回到我尝试解码令牌的网址:
$code = $request->code;
$client = new Google\Client();
$accessToken = $client->fetchAccessTokenWithAuthCode($code);
var_dump($accessToken);
exit;
Run Code Online (Sandbox Code Playgroud)
这给了我这个错误:
array(2) { ["error"]=> string(15) "invalid_request" ["error_description"]=> string(43) "无法根据请求确定客户端 ID。" }
Ben*_*all 11
发现问题了。如果其他人遇到这个问题,这只是因为我在尝试获取访问令牌之前没有加载身份验证配置。
例如,在调用 fetchAccessTokenWithAuthCode() 之前添加此行。$client->setAuthConfig(base_path() . '/client_secret.json');
这个错误让我很困惑,我认为这是检索身份验证令牌的问题。
| 归档时间: |
|
| 查看次数: |
6341 次 |
| 最近记录: |