我正在使用
oauth2
_
flutter客户端软件包
,连接到
通过OAuth 2.0的Coinbase应用编程接口
..。
据我所知,Coinbase使用代码流进行身份验证。这与Github相同。注意这一点很重要,因为我可以使用oauth2成功地登录到Github
_
flutter的客户端包。
为了连接到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页面,我可以登录,如果打印
它显示了我的回购列表。不出所料。
对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部件,因为它填充了nulls。The The The
打印:
HTTP 401 -无效
_
grant提供的授权授权无效、已过期、已吊销、与授权请求中使用的重定向URI不匹配或已颁发给其他客户端。
我曾尝试在Coinbase中创建一个新的OAuth应用程序,但是不起作用。
有人知道我为什么会收到这个错误吗?这让我感到困惑,因为代码使用完全相同的OAuth流与Github一起工作。
发布于 2021-03-02 14:31:50
我使用postman手动测试了身份验证流,这使我能够获得令牌。
经过一些测试,我可以通过添加额外的验证码参数并禁用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');https://stackoverflow.com/questions/66286755
复制相似问题