如何在meteor中使用Google Contacts API? [英] How to use Google Contacts API in meteor?

查看:24
本文介绍了如何在meteor中使用Google Contacts API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用meteor 创建一个网页,其中包含可供选择的Google 群组下拉列表,一旦选择,就会显示Google 联系人.

I am using meteor to create a webpage with a dropdown list of Google Groups to select from and once selected, the Google contacts will be displayed.

我正在使用 HTTP.call POST 到 Google 的 API 并使用来自 mongoDB 的 accessToken 进行测试,但是当我在一段时间后使用该令牌时,它会过期.我研究过实现身份验证流程,但它变得非常复杂,因为 Google 上没有用于meteor 的示例代码.我是 nodeJS、Javascript 和 Meteor 的新手.我会以错误的方式解决这个问题吗?我将如何在流星中实现这一点?

I am using HTTP.call POST to Google's API and testing with the accessToken from mongoDB but when I use that token after some time it expires. I looked into implementing an authentication flow but it is getting very complicated since there is no sample code on Google for meteor. I am new to nodeJS, Javascript and Meteor. Am I going about this the wrong way? How would I implement this in meteor?

https://developers.google.com/accounts/docs/OAuth2?csw=1#expiration

推荐答案

为了处理accessToken过期,需要从Google获取refreshToken.使用此 refreshToken,您可以在需要时通过简单的 HTTP POST 获取一个新的 accessToken 到 Google 的 API.这里是来自 Google 的相关文档.要获取 refreshToken,您需要请求离线访问,并且可能还需要强制批准提示,详见本SO 帖子.

To deal with the expiration of the accessToken, you will need to obtain the refreshToken from Google. With this refreshToken, you can obtain a new accessToken whenever necessary via a simple HTTP POST to Google's API. Here is the relevant documentation from Google. To obtain the refreshToken, you will need to request for offline access and may also need to force the approval prompt, as detailed in this SO post.

forceApprovalPrompt: {google: true},
requestOfflineToken: {google: true},

我建议使用 Meteor 的 HTTP 包来实现上述所有目标.所有的工具都在那里.你可能已经想通了:

I recommend achieving all of the above using Meteor's HTTP package. All the tools are there. You've probably already figured it out:

  var result = HTTP.post(
    "https://www.googleapis.com/oauth2/v3/token",
    {
      params: {
        'client_id': config.clientId,
        'client_secret': config.secret,
        'refresh_token': user.services.google.refreshToken,
        'grant_type': 'refresh_token'
      }
  });

  //Do some error checking here

  var newAccessToken = result.data.access_token;

  1. refresh_token - 授权返回的刷新令牌代码交换.
  2. client_id - 从客户端获取的客户端 ID开发者控制台.
  3. client_secret - 从开发者控制台.
  4. grant_type - 如 OAuth 2.0 中所定义规范,此字段必须包含 refresh_token 的值.
  5. result.data 将是具有以下内容的 JSON 对象

  1. refresh_token - The refresh token returned from the authorization code exchange.
  2. client_id - The client ID obtained from the Developers Console.
  3. client_secret - The client secret obtained from the Developers Console.
  4. grant_type - As defined in the OAuth 2.0 specification, this field must contain a value of refresh_token.
  5. result.data will be a JSON object with the following

{"access_token":"1/fFBGRNJru1FQd44AzqT3Zg","expires_in":3920,"token_type":"承载",}

{ "access_token":"1/fFBGRNJru1FQd44AzqT3Zg", "expires_in":3920, "token_type":"Bearer", }

这篇关于如何在meteor中使用Google Contacts API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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