我遵循了他们的文档,但我没有看到我的错误是什么!
我正在尝试从用户的个人资料中获取信息(可能是姓名性别等...) 我遵循了谷歌网站的所有步骤(在我的例子中,javascript 代码)
一切正常,减去我想对用户配置文件执行的请求!
这个例子是登录google账号后自动执行的回调函数:
function signinCallback(authResult) {
if (authResult['status']['signed_in']) {
// Update the app to reflect a signed in user
// Hide the sign-in button now that the user is authorized, for example:
//document.getElementById('signinButton').setAttribute('style', 'display: none');
console.log(authResult);
gapi.client.setApiKey('API_KEY'); //(That function gaves me an error which is invalid credentials, i did put my API_KEY (from the google console ))
gapi.client.load('plus','v1', function(){
var request = gapi.client.plus.people.get({
'userId': 'me'
});
request.execute(function(resp) {
console.log(resp);
//console.log('Retrieved profile for:' + resp.displayName);
});
});
gapi.auth.signOut(); // logging out !
} else {
// Update the app to reflect a signed out user
// Possible error values:
// "user_signed_out" - User is signed-out
// "access_denied" - User denied access to your app
// "immediate_failed" - Could not automatically log in the user
console.log('Sign-in state: ' + authResult['error']);
}
}
一切都不如 setAPIkeys 和下面的一切! 如果我没有将设置函数和代码放在下面,我会收到 400 错误(“消息”:“已超出未验证使用的每日限制。继续使用需要注册。”)。
所以我对真诚地这样做失去了希望......我认为谷歌的文档很糟糕! 这有什么问题吗?
祝大家圣诞快乐!
附言: 是的,我知道 API key 不能被硬编码,但我真的迷失了谷歌的文档......
最佳答案
可能的解决方案:
尝试删除
gapi.client.setApiKey('API_KEY');
和
gapi.auth.signOut();//注销!
signOut 工作非常快,而 gapi.client.load
是加载 js 代码的异步函数。在执行 gapi.client.load
回调之前,用户可能已注销。
我不确定是否需要:
- 转到 Google Developers Console。
- 选择您的项目。
- 在左侧边栏中,选择 APIs & auth。
- 在显示的 API 列表中,找到 Google+ API 并将其状态设置为开启。
确保您请求范围 https://www.googleapis.com/auth/plus.login或 https://www.googleapis.com/auth/plus.me登录时。
关于javascript - 与谷歌和他的 API 或登录按钮有困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27571833/