使用Google Contacts API 3.0版和NodeJS'Passport同步联系人

时间:2022-08-28 15:26:48

I am using passport, and would like to use the Google Contacts API version 3.0 to sync Google contacts with my application (which would suddenly become 10 times more useful).

我使用的是护照,并希望使用Google Contacts API 3.0版将Google联系人与我的应用程序同步(这会突然变得有用10倍)。

Has anybody done this? If so, do you have some example code? Is it even possible to use passport authentication to get it all working?

有人这样做过吗?如果是这样,你有一些示例代码吗?甚至可以使用护照身份验证来使其全部正常工作吗?

1 个解决方案

#1


17  

This comes in two parts, authorization, and then the actual request.

这有两个部分,授权,然后是实际请求。

It is basically using OAuth2 protocol, where you redirect the client to google url with scopes(You must at least have https://www.google.com/m8/feeds in your scopes to be able to read and write contacts) and your client id/secret(get them by registering your app. Then google will redirect the user back with the access token on the URL.

它基本上使用的是OAuth2协议,您可以将客户端重定向到带有范围的Google网址(您必须至少在示波器中使用https://www.google.com/m8/feeds才能读取和写入联系人)并且客户端ID /机密(通过注册您的应用程序获取它们。然后谷歌将使用URL上的访问令牌重定向用户。

You don't need to do this yourself, because there are different modules that already does this:

您不需要自己执行此操作,因为有不同的模块已经执行此操作:

  • passport-google-oauth

    This makes it easy and assuming you are already using passport, this probably what you want. It is written by the author of passportjs. Just follow the example in it for OAuth 2.0 strategy. Note that you need to you add the right scopes when you are calling passport.authenticate('google', ...). This module when it gets the token, it will get the user profile, so you have to have one of the 3 scopes below:

    这很容易,假设你已经在使用护照,这可能就是你想要的。它由passportjs的作者撰写。只需按照其中的示例进行OAuth 2.0策略即可。请注意,当您调用passport.authenticate('google',...)时,您需要添加正确的范围。该模块获取令牌后,将获取用户配置文件,因此您必须拥有以下3个范围之一:

    passport.authenticate('google', { scope: [ // One of the next three `auth` scopes are needed.
        'https://www.googleapis.com/auth/userinfo.profile',
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/plus.login',
        'https://www.google.com/m8/feeds'
    ] }),
    
  • googleapis

    This is module is officially supported by google and created by google employees. You can use it to authenticate, but sadly it doesn't support gData, which contains google contacts. You can check the example to see how you can get the token. You only need the m8/feeds scope with this module, no need for the other ones if you don't want to get the user profile.

    这个模块由谷歌官方支持并由谷歌员工创建。您可以使用它进行身份验证,但遗憾的是它不支持包含谷歌联系人的gData。您可以查看示例以了解如何获取令牌。您只需要使用此模块的m8 / feeds范围,如果您不想获取用户配置文件,则无需其他模块。

  • gdata-js

    This is a non-popular non-maintaining module, but it is more lightweight than the previous two modules. It might need a little polishing out of the box. I suggest also reading the source for understanding the api right.

    这是一个非流行的非维护模块,但它比前两个模块更轻量级。它可能需要一点点抛光。我建议也阅读理解api权利的来源。

Once you got the tokens, then you go for the slightly easier part, making the requests and getting the data.

一旦你拿到了令牌,那么你就可以找到更容易的部分,发出请求并获取数据。

If you read the docs, it's actually very easy. For example to get all contacts(almost all, it's paginated), you need to make a GET request to this url:

如果您阅读文档,它实际上非常简单。例如,要获取所有联系人(几乎全部,它是分页的),您需要向此URL发出GET请求:

https://www.google.com/m8/feeds/contacts/default/full?alt=json&oauth_token=THE_ACCESS_TOKEN

Again there are many modules that can help you in this.

同样,有许多模块可以帮助您。

  • google-contacts
  • node-gdata
  • gdata-js Read the source to understand it's api. It's pretty easy actually:

    gdata-js阅读源代码以了解它的api。实际上很简单:

    var client = require('gdata-js')(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET);
    client.setToken({ access_token: accessToken, refresh_token: refreshToken });
    client.getFeed('https://www.google.com/m8/feeds/contacts/default/full', function (err, result) { ... });
    

#1


17  

This comes in two parts, authorization, and then the actual request.

这有两个部分,授权,然后是实际请求。

It is basically using OAuth2 protocol, where you redirect the client to google url with scopes(You must at least have https://www.google.com/m8/feeds in your scopes to be able to read and write contacts) and your client id/secret(get them by registering your app. Then google will redirect the user back with the access token on the URL.

它基本上使用的是OAuth2协议,您可以将客户端重定向到带有范围的Google网址(您必须至少在示波器中使用https://www.google.com/m8/feeds才能读取和写入联系人)并且客户端ID /机密(通过注册您的应用程序获取它们。然后谷歌将使用URL上的访问令牌重定向用户。

You don't need to do this yourself, because there are different modules that already does this:

您不需要自己执行此操作,因为有不同的模块已经执行此操作:

  • passport-google-oauth

    This makes it easy and assuming you are already using passport, this probably what you want. It is written by the author of passportjs. Just follow the example in it for OAuth 2.0 strategy. Note that you need to you add the right scopes when you are calling passport.authenticate('google', ...). This module when it gets the token, it will get the user profile, so you have to have one of the 3 scopes below:

    这很容易,假设你已经在使用护照,这可能就是你想要的。它由passportjs的作者撰写。只需按照其中的示例进行OAuth 2.0策略即可。请注意,当您调用passport.authenticate('google',...)时,您需要添加正确的范围。该模块获取令牌后,将获取用户配置文件,因此您必须拥有以下3个范围之一:

    passport.authenticate('google', { scope: [ // One of the next three `auth` scopes are needed.
        'https://www.googleapis.com/auth/userinfo.profile',
        'https://www.googleapis.com/auth/userinfo.email',
        'https://www.googleapis.com/auth/plus.login',
        'https://www.google.com/m8/feeds'
    ] }),
    
  • googleapis

    This is module is officially supported by google and created by google employees. You can use it to authenticate, but sadly it doesn't support gData, which contains google contacts. You can check the example to see how you can get the token. You only need the m8/feeds scope with this module, no need for the other ones if you don't want to get the user profile.

    这个模块由谷歌官方支持并由谷歌员工创建。您可以使用它进行身份验证,但遗憾的是它不支持包含谷歌联系人的gData。您可以查看示例以了解如何获取令牌。您只需要使用此模块的m8 / feeds范围,如果您不想获取用户配置文件,则无需其他模块。

  • gdata-js

    This is a non-popular non-maintaining module, but it is more lightweight than the previous two modules. It might need a little polishing out of the box. I suggest also reading the source for understanding the api right.

    这是一个非流行的非维护模块,但它比前两个模块更轻量级。它可能需要一点点抛光。我建议也阅读理解api权利的来源。

Once you got the tokens, then you go for the slightly easier part, making the requests and getting the data.

一旦你拿到了令牌,那么你就可以找到更容易的部分,发出请求并获取数据。

If you read the docs, it's actually very easy. For example to get all contacts(almost all, it's paginated), you need to make a GET request to this url:

如果您阅读文档,它实际上非常简单。例如,要获取所有联系人(几乎全部,它是分页的),您需要向此URL发出GET请求:

https://www.google.com/m8/feeds/contacts/default/full?alt=json&oauth_token=THE_ACCESS_TOKEN

Again there are many modules that can help you in this.

同样,有许多模块可以帮助您。

  • google-contacts
  • node-gdata
  • gdata-js Read the source to understand it's api. It's pretty easy actually:

    gdata-js阅读源代码以了解它的api。实际上很简单:

    var client = require('gdata-js')(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET);
    client.setToken({ access_token: accessToken, refresh_token: refreshToken });
    client.getFeed('https://www.google.com/m8/feeds/contacts/default/full', function (err, result) { ... });