【问题标题】:OAuth invalid_grant error on coinbase using oauth2_client flutter package使用 oauth2_client 颤振包在 coinbase 上的 OAuth invalid_grant 错误
【发布时间】:2021-02-20 00:34:44
【问题描述】:

我正在使用oauth2_client package for flutter,连接到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 它会显示我的 repos 列表。正如预期的那样。

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

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 提供的授权授权无效, 已过期,已撤销,与 授权请求,或已发给其他客户端。

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

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

【问题讨论】:

  • 只是为了增加神秘感,它现在以德语返回? flutter: HTTP 401 - invalid_grant Der eingegebene Berechtigungsnachweis ist ungültig, abgelaufen, wurde widerrufen oder stimmt nicht mit der in der Autorisierungsanforderung angegebenen umgeleiteten URI überein oder wurde an einen anderen Client ausgegeben.

标签: flutter dart oauth-2.0 coinbase-api


【解决方案1】:

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

经过一些测试,我能够通过添加额外的身份验证代码参数和禁用 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');

【讨论】:

  • 你好@drandom3,我正在尝试进行相同的集成,但在 Android 上不起作用(在 iOS 上完美运行),重定向的最后一部分永远不起作用,我在 Coinbase 中有一个屏幕saiys:继续……(但它永远不会从那里开始)也许你有类似的情况?提前致谢。
  • 对不起@RobinsonRuedaVásquez 我实际上从未在 Android 上测试过。听起来这可能是重定向 URI 的问题,请检查您的 AndroidManifest.xml
【解决方案2】:

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

错误“invalid_grant”

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

也适用于其他oauth 2

谢谢

【讨论】:

    猜你喜欢
    • 2015-01-19
    • 1970-01-01
    • 2022-06-14
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 2019-12-18
    • 2021-07-04
    • 2018-10-30
    相关资源
    最近更新 更多