谷歌和他的API或登录按钮有一个难题

Having an hard issues with google and his API or login buttons

本文关键字:登录 按钮 有一个 难题 API 谷歌      更新时间:2023-09-26

我遵循了他们的文档,但我看不出我的错误是什么!

我正在尝试从用户的个人资料中获取信息(可能是姓名性别等...我遵循了谷歌网站的所有步骤(在我的例子中,是javascript代码(

一切正常,而不是我想对用户配置文件执行的请求!

这个例子是登录谷歌账户后自动执行的回调函数

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']);
  }
}

一切都比设置API键和下面的所有内容工作少!如果我不输入下面的 set 函数和代码,我会收到一个 400 错误("消息":"超出未经身份验证使用的每日限制。继续使用需要注册。

所以我对真诚地这样做失去了希望......我认为谷歌的文档很糟糕!这是怎么回事?

祝大家圣诞快乐!

PS :是的,我知道 API 密钥不是硬编码的,但我对谷歌的文档感到非常迷茫......

可能的解决方案:


尝试删除

gapi.client.setApiKey('API_KEY');

gapi.auth.signOut(); // logging out !

signOut工作速度非常快,而gapi.client.load 是加载js代码的异步函数。用户可能在执行回调之前注销gapi.client.load


我不确定这是否是必需的:

  1. 转到 Google 开发者控制台。
  2. 选择您的项目。
  3. 在左侧边栏中,选择"API 和身份验证"。
  4. 在显示的 API 列表中,找到 Google+ API 并将其状态设置为"开"。

请确保在登录时请求范围 https://www.googleapis.com/auth/plus.login 或 https://www.googleapis.com/auth/plus.me。