OAuth invalid_grant 使用 oauth2_client flutter 包的 coinbase 错误

OAuth invalid_grant error on coinbase using oauth2_client flutter package

我正在使用 oauth2_client package for flutter, connecting to the Coinbase API via OAuth 2.0

据我所知,Coinbase 使用代码流进行身份验证。这与 Github 相同。需要注意这一点很重要,因为我可以使用 oauth2_client 包成功验证 Github。

要连接到 Github 我使用了现有的客户端:

import 'package:oauth2_client/oauth2_client.dart';
import 'package:meta/meta.dart';

/// Implements an OAuth2 client against GitHub
///
/// In order to use this client you need to first create a new OAuth2 App in the GittHub Developer Settings (https://github.com/settings/developers)
///
class GitHubOAuth2Client extends OAuth2Client {
  GitHubOAuth2Client(
      {@required String redirectUri, @required String customUriScheme})
      : super(
            authorizeUrl: 'https://github.com/login/oauth/authorize',
            tokenUrl: 'https://github.com/login/oauth/access_token',
            redirectUri: redirectUri,
            customUriScheme: customUriScheme) {
    accessTokenRequestHeaders = {'Accept': 'application/json'};
  }
}

然后我创建了一个在应用程序中调用的方法:


void _oauthMethod() async {
    //clientID
    String cID = 'x';

    //clientSecret
    String cSecret = 'y';

    OAuth2Client client = GitHubOAuth2Client(
        redirectUri: 'my.app://oauth2redirect', customUriScheme: 'my.app');

    AccessTokenResponse tknResp = await client.getTokenWithAuthCodeFlow(
        clientId: cID, clientSecret: cSecret, scopes: ['repo']);

    http.Response resp = await http.get('https://api.github.com/user/repos',
        headers: {'Authorization': 'Bearer ' + tknResp.accessToken});
   
  }

调用此函数会打开 Github 的 OAuth 页面,我可以登录,如果我打印 resp,它会显示我的存储库列表。不出所料。

对 Coinbase 使用相同的方法,我首先创建新的 class:

class MyOAuth2Client extends OAuth2Client {
  MyOAuth2Client(
      {@required String redirectUri, @required String customUriScheme})
      : super(
            authorizeUrl:
                'https://www.coinbase.com/oauth/authorize', //Your service's authorization url
            tokenUrl:
                'https://api.coinbase.com/oauth/token', //Your service access token url
            redirectUri: redirectUri,
            customUriScheme: customUriScheme) {
    this.accessTokenRequestHeaders = {'Accept': 'application/json'};
  }
}

然后我创建调用的方法:

void _coinbaseAuth() async {
    String cID = 'x';
    String cSecret = 'y';
    MyOAuth2Client client = MyOAuth2Client(
        redirectUri: 'my.app://oauth2redirect', customUriScheme: 'my.app');

    AccessTokenResponse tknResp = await client.getTokenWithAuthCodeFlow(
        clientId: cID, clientSecret: cSecret, scopes: ['wallet:user:read']);

    print(tknResp);


    //code fails 
    //http.Response resp =
    //        await http.get('https://api.coinbase.com/v2/user', headers: {
    //      'Authorization': 'Bearer ' + tknResp.accessToken,
    //      'Content-Type': 'application/json',
    //      'Charset': 'utf-8'
    //    });


  }

我不能 运行 http.Response 部分,因为它充满了空值。 tknResp 打印:

HTTP 401 - invalid_grant The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.

我曾尝试在 Coinbase 中创建一个新的 OAuth 应用程序,但这不起作用。

有谁知道我为什么会收到此错误?这让我感到困惑,因为代码与 Github 使用完全相同的 OAuth 流程。

我使用邮递员手动测试了身份验证流程,这使我能够获得令牌。

经过一些测试,我能够通过添加额外的授权代码参数并禁用 PKCE 来获取带有 dart 包的令牌

    AccessTokenResponse tknResp = await client.getTokenWithAuthCodeFlow(
        clientId: cID,
        clientSecret: cSecret,
        scopes: ["wallet:user:read"],
        authCodeParams: {
          "grant_type": "authorization_code",
          "redirect_uri": "my.app://oauth2redirect"
        },
        enablePKCE: false,
        state: 'OYWjs_95M6jlkvy5');

你好,在我的情况下,我在使用我的文本应用程序使用 coinbase 的重复 oauth 获取令牌时遇到问题。

错误“invalid_grant”

为了解决,我转到测试帐户并导航到活动部分,然后单击 x (x) 退出并再次打算。

也适用于其他oauth 2

谢谢