【发布时间】:2015-03-15 10:50:46
【问题描述】:
我正在尝试通过 Google 的 OAuth 进行身份验证,但在建立与他们的 API 的连接时遇到问题
我的客户代码:
'click #addChannel': function (event) {
event.preventDefault();
var userId = Meteor.userId();
var options = {
requestPermissions: [
'https://www.googleapis.com/auth/youtube',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/youtube.force-ssl',
'https://www.googleapis.com/auth/youtube.readonly',
'https://www.googleapis.com/auth/youtube.upload',
'https://www.googleapis.com/auth/youtubepartner',
'https://www.googleapis.com/auth/youtubepartner-channel-audit',
],
requestOfflineToken: true
};
Google.requestCredential(options, function(token) {
Meteor.call('userAddOauthCredentials', userId, token, function(error, result) {
if (error) {
throw error;
}
console.log(result);
});
});
我的服务器代码:
userAddOauthCredentials: function(userId, token) {
check(userId, String);
check(token, String);
var config = ServiceConfiguration.configurations.findOne({service: 'google'});
if (!config) {
throw new ServiceConfiguration.ConfigError();
}
console.log(token, config);
var endpoint = 'https://accounts.google.com/o/oauth2/token';
var params = {
code: token,
client_id: config.clientId,
client_secret: OAuth.openSecret(config.secret),
redirect_uri: OAuth._redirectUri('google', config),
grant_type: 'authorization_code',
};
try { <------------------------------------------------------ this fails
response = HTTP.post(endpoint, { params: params });
} catch (err) {
throw _.extend(new Error("(first) Failed to complete OAuth handshake with Google. " + err.message),
{response: err.response});
}
if (response.data.error) { // if the http response was a json object with an error attribute
throw new Error("(second) Failed to complete OAuth handshake with Google. " + response.data);
} else {
return {
accessToken: response.data.access_token,
refreshToken: response.data.refresh_token,
expiresIn: response.data.expires_in,
idToken: response.data.id_token
};
}
上面会抛出一个[400] { "error" : "invalid_grant" } 错误。
上面的大部分代码都是我从流星帐户-google 包如何登录用户中获得的(在我的应用程序中运行良好)。链接到那个:
关于如何从这里开始的任何建议?
非常感谢
更新1:
我在日志中收到这些警告
W20150318-09:11:42.532(1) (oauth_server.js:71) Unable to base64 decode state from OAuth query: undefined
W20150318-09:11:42.532(1) (oauth_server.js:71) Unable to base64 decode state from OAuth query: undefined
W20150318-09:11:42.533(1) (oauth_server.js:71) Unable to base64 decode state from OAuth query: undefined
W20150318-09:11:42.534(1) (oauth_server.js:398) Error in OAuth Server: Match error: Expected string, got undefined
【问题讨论】:
-
只是确保,但您已经生成了自己的 clientID 和 clientSecret,对吗? 400 错误通常是由于请求中的参数不正确,因此我建议尝试跟踪导致错误的方法并确保正确调用它。
-
是的,我已经生成了这些。此外,生成错误的不是我的应用程序,而是从谷歌 API 返回的
-
您是否碰巧对 google OAuth 的查询有任何网络跟踪?另外,您是否从客户那里获得了授权码,并且在代码交换期间看到了错误?
-
我刚刚注意到我的服务器日志中出现了一些奇怪的警告,已更新
-
运气好了吗?我遇到了同样的问题,并且已经尝试解决了几个小时。发现一些旧的 SO 帖子说,尽管命名,我们需要提供一个服务电子邮件作为 client_id。试过了,但仍然没有运气(这些帖子来自 2013 年,因此可能已经修复了特定问题)。
标签: google-maps-api-3 meteor oauth google-api google-oauth