“需要登录"错误.使用 Google API 获取 oauth2 令牌后如何登录 [英] 'Login Required' error. How to login after getting oauth2 token with Google API

查看:75
本文介绍了“需要登录"错误.使用 Google API 获取 oauth2 令牌后如何登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个小型 Node/React 应用程序,我试图在其中实施 OAuth2 Google 以请求 Google Analytics API.我正在使用 Passport.js 来处理身份验证.

I'm building a small Node/React app where I'm trying to implement OAuth2 Google to request the Google Analytics API. I'm using Passport.js to handle the authentification.

我能够正确获取令牌:

router.get(
    "/auth/google/callback",
    passport.authenticate("google", { failureRedirect: "/error", session: false }),
    function(req, res) {
        var token = req.user.token;
        res.redirect("http://localhost:5000/?token=" + token);
    }
);

但是,在尝试调用 Google Analytics 管理 API 时出现以下错误错误:需要登录.

However, when trying to call the Google Analytics management API I have the following error Error: Login Required.

router.get('/getData', function(req, res) {
    googleAccounts.management.profiles.list(
      {
        accountId: '~all',
        webPropertyId: '~all'
      },
      (err, data) => {
        if (err) {
          console.error('Error: ' + err)
          res.send('An error occurred')
        } else if (data) {

          Console.log(data)

        }
      }
    )
})

我如何登录?我遗漏了哪一步?

How do I login? What step I'm missing?

推荐答案

您不会发送访问令牌,除非您使用 gapi 的内置身份验证,否则您必须在 HTTP 授权标头中手动发送访问令牌.

You aren’t sending the access token, unless you’re using the gapi’s inbuilt auth, you have to send the access token manually in a HTTP authorization header.

请使用以下代码段来实现相同的效果.

Please use the following snippet to achieve the same.

function listViews() {
  gapi.client.setToken{
    access_token: <access-token>
  })
  ... rest of the function

这篇关于“需要登录"错误.使用 Google API 获取 oauth2 令牌后如何登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆