【问题标题】:How to authenticate in googleapi?如何在谷歌 api 中进行身份验证?
【发布时间】:2019-02-06 10:55:17
【问题描述】:

好的,我已经收到了所有的令牌,我想访问https://www.googleapis.com/admin/directory/v1/users/

我该怎么做 - 我试过了:

await (await fetch("https://www.googleapis.com/admin/directory/v1/users/" +
            userInfo.sub + '?access_token=' + accessToken, {method: "GET"})).json()

但它没有告诉我我没有登录 (401)。

【问题讨论】:

  • 401 我们能得到什么完整的错误信息。您是否考虑过使用 google apis js 客户端库而不是自己做这个?
  • 它说“需要登录”。我不能使用 api js 库,因为首先我不想其次我正在使用节点。
  • 首先,如果您使用的是节点,为什么问题标记为 JavaScript 而不是 node.js。其次为什么不使用谷歌apis node.js 客户端库,而不是自己做。
  • 因为我使用的是普遍适用于任何地方的 fetch。但我不想使用 googleapis。
  • Directory 是一个 Google API (GoogleAPIs) 令我印象深刻的是,您无需使用 Google 客户端库即可获得访问令牌。如果我的回答对您没有帮助,请添加您的验证码。

标签: javascript node.js google-api token access-token


【解决方案1】:

尝试将其添加为不记名令牌

Javascript

fetch('https://www.googleapis.com/admin/directory/v1/users', { 
   method: 'get', 
   headers: new Headers({
     'Authorization': 'Bearer ' + accessToken, 
     'Content-Type': 'application/x-www-form-urlencoded'
   })
 });

Node.js

const request = require('request');

request({
  url: 'https://www.googleapis.com/admin/directory/v1/users',
  headers: {
     'Authorization': 'Bearer ' + accessToken
  },
  rejectUnauthorized: false
}, function(err, res) {
      if(err) {
        console.error(err);
      } else {
        console.log(res.body);
      }

});

【讨论】:

    猜你喜欢
    • 2023-03-13
    • 2022-11-08
    • 2021-05-07
    • 1970-01-01
    • 2012-07-23
    • 2020-09-26
    • 2010-10-16
    • 1970-01-01
    • 2021-10-14
    相关资源
    最近更新 更多