"unauthorized_client" 使用 PHP 客户端使用 Google API 客户端刷新令牌时出错?
"unauthorized_client" error when using PHP client to refreshToken with Google API client?
我请求使用 GoogleSignIn pod 中的此代码从 IOS 应用程序访问用户数据
[GIDSignIn sharedInstance].uiDelegate = self;
[[GIDSignIn sharedInstance] setScopes:@[ ... ];
[[GIDSignIn sharedInstance] signInSilently];
然后我在 appDelegate 中收到 accessToken 和 refreshToken 并将其发送到服务器 运行 PHP 并使用此 google 客户端库 (https://github.com/googleapis/google-api-php-client) ...第一个小时一切正常,访问令牌有效,允许我访问所有范围。然后一小时后访问令牌过期,我尝试使用此代码刷新它
$client = new Google_Client();
$client->setApplicationName(<app name>);
$client->setDeveloperKey(<google_oauth_developer_key>);
$client->setClientSecret(<google_oauth_client_secret>);
$client->setClientId(<google_oauth_client_id>);
$client->refreshToken("1/12ju3ZZim-r6co195lIVNNvzBq9x5G64GJCWkYsOQ18");
和上面的"refreshToken"函数returns这个错误
=> [
"error" => "unauthorized_client",
"error_description" => "Unauthorized",
]
值得注意的是 app name
、google_oauth_developer_key
、google_oauth_client_secret
和 google_oauth_client_id
没有改变。它们与早些时候在访问令牌尚未过期时用于检索用户的联系人、日历事件等的相同。
可能导致此错误的原因是什么?
您需要在某个时候实际使用您的刷新令牌。
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->getAccessToken();
从我的 Oauth2Authentication.php 示例中窃取的代码
备注
另请记住,刷新令牌是基于用户/客户端的。因此,如果令牌是由一个客户端生成的,那么另一个客户端可能无法使用它。如果您可以在 php 应用程序中使用在 Ios 代码中创建的刷新令牌,我将感到非常惊讶。客户类型不同。
好的,我知道我做错了什么了。我最初遵循这些说明 https://developers.google.com/identity/sign-in/ios/sign-in which assume you need online access only, but I actually need offline access to the scopes so I followed these instructions https://developers.google.com/identity/sign-in/ios/offline-access,现在可以使用了。因此,不要让 IOS 应用像这样分别发送访问令牌和刷新令牌
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)error {
if (!error) {
NSString *accessToken = user.authentication.accessToken;
NSString *refreshToken = user.authentication.refreshToken;
我只是让它像这样发送 serverAuthCode
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)error {
if (!error) {
NSString *serverAuthCode = user.serverAuthCode;
然后在服务器上我运行此代码获取访问令牌
$access_token = $client->authenticate($google_server_auth_code);
$client->setAccessToken($access_token);
然后当访问令牌过期时我运行刷新它
$access_token = $client->refreshToken($client->getRefreshToken());
$client->setAccessToken($access_token);
我请求使用 GoogleSignIn pod 中的此代码从 IOS 应用程序访问用户数据
[GIDSignIn sharedInstance].uiDelegate = self;
[[GIDSignIn sharedInstance] setScopes:@[ ... ];
[[GIDSignIn sharedInstance] signInSilently];
然后我在 appDelegate 中收到 accessToken 和 refreshToken 并将其发送到服务器 运行 PHP 并使用此 google 客户端库 (https://github.com/googleapis/google-api-php-client) ...第一个小时一切正常,访问令牌有效,允许我访问所有范围。然后一小时后访问令牌过期,我尝试使用此代码刷新它
$client = new Google_Client();
$client->setApplicationName(<app name>);
$client->setDeveloperKey(<google_oauth_developer_key>);
$client->setClientSecret(<google_oauth_client_secret>);
$client->setClientId(<google_oauth_client_id>);
$client->refreshToken("1/12ju3ZZim-r6co195lIVNNvzBq9x5G64GJCWkYsOQ18");
和上面的"refreshToken"函数returns这个错误
=> [
"error" => "unauthorized_client",
"error_description" => "Unauthorized",
]
值得注意的是 app name
、google_oauth_developer_key
、google_oauth_client_secret
和 google_oauth_client_id
没有改变。它们与早些时候在访问令牌尚未过期时用于检索用户的联系人、日历事件等的相同。
可能导致此错误的原因是什么?
您需要在某个时候实际使用您的刷新令牌。
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
$client->getAccessToken();
从我的 Oauth2Authentication.php 示例中窃取的代码
备注
另请记住,刷新令牌是基于用户/客户端的。因此,如果令牌是由一个客户端生成的,那么另一个客户端可能无法使用它。如果您可以在 php 应用程序中使用在 Ios 代码中创建的刷新令牌,我将感到非常惊讶。客户类型不同。
好的,我知道我做错了什么了。我最初遵循这些说明 https://developers.google.com/identity/sign-in/ios/sign-in which assume you need online access only, but I actually need offline access to the scopes so I followed these instructions https://developers.google.com/identity/sign-in/ios/offline-access,现在可以使用了。因此,不要让 IOS 应用像这样分别发送访问令牌和刷新令牌
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)error {
if (!error) {
NSString *accessToken = user.authentication.accessToken;
NSString *refreshToken = user.authentication.refreshToken;
我只是让它像这样发送 serverAuthCode
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)error {
if (!error) {
NSString *serverAuthCode = user.serverAuthCode;
然后在服务器上我运行此代码获取访问令牌
$access_token = $client->authenticate($google_server_auth_code);
$client->setAccessToken($access_token);
然后当访问令牌过期时我运行刷新它
$access_token = $client->refreshToken($client->getRefreshToken());
$client->setAccessToken($access_token);