在nodejs上使用googleapi JWT时出现CORS错误

时间:2018-01-23 13:25:58

标签: javascript node.js jwt google-analytics-api

我正在尝试使用googleapis授权使用JWT,以便能够访问分析数据,但我正在打算

POST https://accounts.google.com/o/oauth2/token 400 ()

Failed to load https://accounts.google.com/o/oauth2/token: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 400. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

TypeError: Failed to fetch

当我运行此代码时。

import google from 'googleapis'
const key = require('../../../static/key.json')

export const GA = {
  methods: {
    getVisits () {
      var jwtClient = new google.auth.JWT(
        key.client_email,
        null,
        key.private_key,
        ['https://www.googleapis.com/auth/analytics.readonly'] // an array of auth scopes
      )

      jwtClient.authorize(function (err, tokens) {
        if (err) {
          console.log(err)
          // return
        } else {
          console.log('Successfully connected!')
        }
       })
    }
  }

}

我正在尝试使用在开发人员控制台中的项目上设置的serviceaccount来访问数据。

我看不出我错过了什么。

1 个答案:

答案 0 :(得分:0)

我通过调用server.js脚本中的脚本来修复它。我的错!