【发布时间】: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