【问题标题】:How to fix the "Malformed auth code" when trying to refreshToken on the second attempt?如何在第二次尝试 refreshToken 时修复“格式错误的身份验证代码”?
【发布时间】:2020-02-01 05:11:18
【问题描述】:

我正在开发一个带有 Angular 和 Cordova 插件的 Android 应用程序,我想将它与 Google 身份验证集成。 我已经安装了cordova-plugin-googleplus 并成功集成到应用程序中。 当用户登录时,我会收到一个响应,我可以在其中获取 accessToken、配置文件用户信息和 refreshToken。

现在我想实现一个功能来刷新令牌,而不用每小时用一个新的提示屏幕打扰用户。

我已经成功更新了accessToken,但它只能在第一次使用

我用过这两种方式:

  1. 使用以下数据发送 curl 请求
curl -X POST \
  'https://oauth2.googleapis.com/token?code=XXXXXXXXXXXXXXXX&client_id=XXXXXXXXXXXXXXXX.apps.googleusercontent.com&client_secret=YYYYYYYYYYYY&grant_type=authorization_code' \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/x-www-form-urlencoded'
  1. 使用适用于 Java 的 Google API 客户端库在服务器端实现它,主要遵循这些 code

重点是当用户第一次登录时(使用cordova-plugin-googleplus),我会收到一个这种格式的refreshToken

4/rgFU-hxw9QSbfdj3ppQ4sqDjK2Dr3m_YU_UMCqcveUgjIa3voawbN9TD6SVLShedTPveQeZWDdR-Sf1nFrss1hc

如果一段时间后我尝试以上述任何一种方式刷新令牌,我会得到一个成功的响应,其中包含一个新的 accessToken 和一个新的 refreshToken。而新的 refreshToken 有这种其他格式

1/FTSUyYTgU2AG8K-ZsgjVi6pExdmpZejXfoYIchp9KuhtdknEMd6uYCfqMOoX2f85J

在第二次尝试更新令牌时,我将令牌替换为第一个请求中返回的令牌

curl -X POST \
  'https://oauth2.googleapis.com/token?code=1/FTSUyYTgU2AG8K-ZsgjVi6pExdmpZejXfoYIchp9KuhtdknEMd6uYCfqMOoX2f85J&client_id=XXXXXXXXXXXXXXXX.apps.googleusercontent.com&client_secret=YYYYYYYYYYYY&grant_type=authorization_code' \
  -H 'Cache-Control: no-cache' \
  -H 'Content-Type: application/x-www-form-urlencoded'

但这一次,两种方式(Curl 和 Java)我都遇到了同样的错误。

{
  "error" : "invalid_grant",
  "error_description" : "Malformed auth code."
}

我在thread 上看到了将 clientId 指定为电子邮件的问题,但我还没有发现如何解决它,因为第一次登录是使用客户端 ID 'XXXXXXX.apps.googleusercontent.com ' 如果我从谷歌帐户设置了一封电子邮件,它会说这是一个“未知的 Oauth 客户端”

我希望任何人都可以帮助我解决这个问题,因为我被困了好几天

【问题讨论】:

    标签: cordova oauth-2.0 cordova-plugins google-oauth google-oauth-java-client


    【解决方案1】:

    最后,我实现了根据需要随时刷新访问令牌。问题在于对 Google Api 如何工作的误解。

    第一次更新令牌时,需要使用这些参数调用此端点,并将从同意屏幕调用(serverAuthCode)的响应中获得的值设置为{{refreshToken}}

    https://oauth2.googleapis.com/token?code={{refreshToken}}&client_id={{googleClientId}}&client_secret={{googleClientSecret}}&grant_type=authorization_code
    

    第一次刷新后,令牌的任何更新都需要通过将第一次调用的响应中获得的属性 {{refresh_token}} 设置为 {{tokenUpdated}} 来调用到另一个端点。

    https://oauth2.googleapis.com/token?refresh_token={{tokenUpdated}}&client_id={{googleClientId}}&client_secret={{googleClientSecret}}&grant_type=refresh_token
    

    这里我给你看一个我的 AuthenticationService 的例子

    import { Injectable} from '@angular/core';
    import { Router } from '@angular/router';
    import { GooglePlus } from '@ionic-native/google-plus/ngx';
    
    @Injectable({
      providedIn: 'root'
    })
    export class AuthenticationService {
    
    
    static AUTH_INFO_GOOGLE = 'auth-info-google';
    static CLIENT_ID = 'XXXXX-XXXX.apps.googleusercontent.com';
    static CLIENT_SECRET = 'SecretPasswordClientId';
    
    
    public authenticationState = new BehaviorSubject(false);
    
      constructor(
        private router: Router,
        private googlePlus: GooglePlus) {
    
      }
    
    public isAuthenticated() {
        return this.authenticationState.value;
    }
    
    public logout(): Promise<void> {
        this.authenticationState.next(false);   
        return this.googlePlus.disconnect()
        .then(msg => {
          console.log('User logged out: ' + msg);
        }, err => {
          console.log('User already disconected');
        }); 
    }
    
    /**
    * Performs the login
    */
    public async login(): Promise<any> {
        return this.openGoogleConsentScreen().then(async (user) => {
          console.log(' ServerAuth Code: ' + user.serverAuthCode);
          user.updated = false;
          await this.setData(AuthenticationService.AUTH_INFO_GOOGLE, JSON.stringify(user));
          this.authenticationState.next(true);
          // Do more staff after successfully login
        }, err => {
            this.authenticationState.next(false);
            console.log('An error ocurred in the login process: ' + err);
            console.log(err);
        });
    
    }
    
      /**
       * Gets the Authentication Token
       */
    public async getAuthenticationToken(): Promise<string> {
          return this.getAuthInfoGoogle()
            .then(auth => {
              if (this.isTokenExpired(auth)) {
                return this.refreshToken(auth);
              } else {
                return 'Bearer ' + auth.accessToken;
              }
            });
    }
    
    
    
    private async openGoogleConsentScreen(): Promise<any> {
      return this.googlePlus.login({
        // optional, space-separated list of scopes, If not included or empty, defaults to `profile` and `email`.
        'scopes': 'profile email openid',
        'webClientId': AuthenticationService.CLIENT_ID,
        'offline': true
      });
    }
    
    private isTokenExpired(auth: any): Boolean {
        const expiresIn = auth.expires - (Date.now() / 1000);
         const extraSeconds = 60 * 59 + 1;
        // const extraSeconds = 0;
        const newExpiration = expiresIn - extraSeconds;
         console.log('Token expires in ' + newExpiration + ' seconds. Added ' + extraSeconds + ' seconds for debugging purpouses');
        return newExpiration < 0;
    }
    
    private async refreshToken(auth: any): Promise<any> {
          console.log('The authentication token has expired. Calling for renewing');
          if (auth.updated) {
            auth = await this.requestGoogleRefreshToken(auth.serverAuthCode, auth.userId, auth.email);
          } else {
            auth = await this.requestGoogleAuthorizationCode(auth.serverAuthCode, auth.userId, auth.email);
          }
          await this.setData(AuthenticationService.AUTH_INFO_GOOGLE, JSON.stringify(auth));
          return 'Bearer ' + auth.accessToken;
    }
    
    
    private getAuthInfoGoogle(): Promise<any> {
        return this.getData(AuthenticationService.AUTH_INFO_GOOGLE)
        .then(oauthInfo => {
          return JSON.parse(oauthInfo);
        }, err => {
          this.clearStorage();
          throw err;
        });
    }
    
    private async requestGoogleAuthorizationCode(serverAuthCode: string, userId: string, email: string): Promise<any> {
        let headers = new HttpHeaders();
        headers = headers.set('Content-Type', 'application/x-www-form-urlencoded');
        let params: HttpParams = new HttpParams();
        params = params.set('code', serverAuthCode);
        params = params.set('client_id', AuthenticationService.CLIENT_ID);
        params = params.set('client_secret', AuthenticationService.CLIENT_SECRET);
        params = params.set('grant_type', 'authorization_code');
        const options = {
          headers: headers,
          params: params
        };
        const url = 'https://oauth2.googleapis.com/token';
        const renewalTokenRequestPromise: Promise<any> = this.http.post(url, {}, options).toPromise()
          .then((response: any) => {
            const auth: any = {};
            auth.accessToken = response.access_token;
            console.log('RefreshToken: ' + response.refresh_token);
            auth.serverAuthCode = response.refresh_token;
            auth.expires = Date.now() / 1000 + response.expires_in;
            auth.userId = userId;
            auth.email = email;
            auth.updated = true;
            return auth;
          }, (error) => {
            console.error('Error renewing the authorization code: ' + JSON.stringify(error));
            return {};
          });
        return await renewalTokenRequestPromise;
    }
    
    private async requestGoogleRefreshToken(serverAuthCode: string, userId: string, email: string): Promise<any> {
        let headers = new HttpHeaders();
        headers = headers.set('Content-Type', 'application/x-www-form-urlencoded');
        let params: HttpParams = new HttpParams();
        params = params.set('refresh_token', serverAuthCode);
        params = params.set('client_id', AuthenticationService.CLIENT_ID);
        params = params.set('client_secret', AuthenticationService.CLIENT_SECRET);
        params = params.set('grant_type', 'refresh_token');
        const options = {
          headers: headers,
          params: params
        };
        const url = 'https://oauth2.googleapis.com/token';
        const renewalTokenRequestPromise: Promise<any> = this.http.post(url, {}, options).toPromise()
          .then((response: any) => {
            const auth: any = {};
            auth.accessToken = response.access_token;
            console.log('RefreshToken: ' + serverAuthCode);
            auth.serverAuthCode = serverAuthCode;
            auth.expires = Date.now() / 1000 + response.expires_in;
            auth.userId = userId;
            auth.email = email;
            auth.updated = true;
            return auth;
          }, (error) => {
            console.error('Error renewing refresh token: ' + JSON.stringify(error));
            return {};
          });
        return await renewalTokenRequestPromise;
    }
    
    private setData(key: string, value: any): Promise<any> {
        console.log('Store the value at key entry in the DDBB, Cookies, LocalStorage, etc')
    }
    
    private getData(key: string): Promise<string> {
        console.log('Retrieve the value from the key entry from DDBB, Cookies, LocalStorage, etc')
    }
    
    private clearStorage(): Promise<string> {
        console.log('Remove entries from DDBB, Cookies, LocalStorage, etc related to authentication')
    }
    
    
    
    }
    

    【讨论】:

      【解决方案2】:

      就我而言,这很愚蠢:google api 更改请求之间的身份验证代码编码。

      第 1 步 - 在获取令牌的第一个请求期间,谷歌返回非常正常的,而不是编码字符串作为代码。

      第 2 步 - 在获取令牌的第二个和第 N 个请求期间(如果它们未被撤销),谷歌将身份验证代码返回为 url 编码。在我的情况下,杀戮变化是 '/' -> '%2F'。

      解决方案: 始终在将身份验证代码交换为访问令牌之前对其进行 URL 解码!

      【讨论】:

      • 是的,%2F 部分引起了我的注意,但在阅读您的帖子之前,我认为我应该盲目地接受代码。感谢您的确认。
      • 谢谢!!!这是我三天后的最后一步!
      【解决方案3】:

      你需要像这样解码你的代码

          String code = "4%2F0AX************...";
          String decodedCode = "";
          try {
            decodedCode = java.net.URLDecoder.decode(code, StandardCharsets.UTF_8.name());
          } catch (UnsupportedEncodingException e) {
            //do nothing
          }
      

      然后使用decodedCode作为参数

      【讨论】:

        【解决方案4】:

        这是一个授权授予流程。为简单起见,以下是它所遵循的步骤。

        1. 第一个请求获得授权码(这个带有参数授权码)
        2. 收到代码后,使用它来获取 access_token 和 refresh_token
        3. 访问令牌过期一段时间后,使用步骤 2 中的 refresh_token 获取新的访问令牌和新的刷新令牌。 (当您使用第 1 步中的代码时,您会看到错误。)

        希望对您有所帮助,请更改您的代码并重试。

        【讨论】:

        • 亲爱的 Venkatesh, 感谢您的回答。尽管如此,我还是按照您提到的步骤进行了操作,但它们与我之前提到的步骤没有什么不同。 * 第 1 步:我从 Cordova Google+ 插件获取此授权代码 * 第 2 步:我向oauth2.googleapis.com/token 发帖并收到以“1/”开头的刷新令牌 * 第 3 步:我重复与第 2 步相同的请求,但更改第 2 步中获得的 refresh_token
        【解决方案5】:

        您的代码都以“/”作为第二个字符。您可能应该在将其放入查询字符串之前对其进行 url 编码。

        【讨论】:

          【解决方案6】:

          通过添加 URLDecode 将起作用。但现在它不再工作了。

          必须添加prompt=consent,然后在使用授权码申领时才返回刷新令牌。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-10-09
            • 1970-01-01
            • 2014-10-07
            • 2022-10-21
            • 1970-01-01
            • 1970-01-01
            • 2023-03-13
            • 1970-01-01
            相关资源
            最近更新 更多