Google操作 - 使用访问令牌访问Calendar API失败 [英] Google Actions - Access to Calendar API with access token fails

查看:255
本文介绍了Google操作 - 使用访问令牌访问Calendar API失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我按照指示在这篇伟大的文章中,设置我的应用程序的服务器和Google操作之间的帐户链接。

I followed the instructions in this great post, to setup account linking between my app's server and google actions.

在auth过程中,我要求 https://www.googleapis.com/auth/calendar 作用域权限。

In the auth process, I requested "https://www.googleapis.com/auth/calendar" scope permission.

我通过调用

I managed to get the auth token on my server by calling

app.getUser().accessToken

但是,当我向googleapi日历提出请求时,请使用以下代码:

But when I make a request to googleapi calendar, using this piece of code:

const google = require('googleapis');
var calendar = google.calendar('v3');

var eventData = {
    auth: myAuthToken,
    calendarId: 'primary',
    resource: {
        'summary': 'My Event',
        'description': 'Event desc',
        'start': {
            'dateTime': '2017-06-11',
        },
        'transparency': 'transparent',
        'visibility': 'private',
        'colorId': 'blue'
    }
};

calendar.events.insert(eventData, 
  function(err, event) {
    if (err) {
      console.log(err)
    }
});

我得到这个错误:

I get is this error:

{ Error: Login Required
    at Request._callback (\node_modules\google-auth-library\lib\transporters.js:85:15)
    at Request.self.callback (\node_modules\request\request.js:188:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (\node_modules\request\request.js:1171:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at IncomingMessage.<anonymous> (\node_modules\request\request.js:1091:12)
    at IncomingMessage.g (events.js:291:16)
    at emitNone (events.js:91:20)
  code: 401,
  errors:
   [ { domain: 'global',
       reason: 'required',
       message: 'Login Required',
       locationType: 'header',
       location: 'Authorization' } ] }

我应该遵循的额外验证步骤?

Is there any additional authentication steps I should follow?

推荐答案

我认为问题在于您使用的结构 auth 参数不正确。你传递一个字符串标记,而它应该是一个OAuth2对象。请参阅 https://github.com/google/google-api -nodejs-client#making-authenticated-requests 以获取详细信息,但总之您需要:

I think the issue is that the structure of what you're using as the auth parameter isn't correct. You're passing it a string token, while it should be an OAuth2 object. See https://github.com/google/google-api-nodejs-client#making-authenticated-requests for details, but in short you will need to:


  1. 创建一个OAuth2 object




var OAuth2 = google.auth.OAuth2;
var oauth2Client = new OAuth2(
  YOUR_CLIENT_ID,
  YOUR_CLIENT_SECRET,
  YOUR_REDIRECT_URL
);




  1. 设置凭证(访问令牌)。




oauth2Client.setCredentials({
  access_token: 'ACCESS TOKEN HERE'
});




  1. 使用 oauth2Client在您的事件数据/调用中 auth 参数中对象。

  1. Use this oauth2Client object in the auth parameter in your event data / call.




var eventData = {
    auth: oauth2Client,
    ...
};

这篇关于Google操作 - 使用访问令牌访问Calendar API失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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