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

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

问题描述

我正在使用流星创建一个带有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上没有流星的示例代码,因此它变得非常复杂。我是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,您可以在必要时通过对Google API的简单HTTP POST获得新的accessToken。 此处是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's实现以上所有功能 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", }

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

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