如何使用带有OAuth v2 UserCredentials的Google Contact API v3创建ContactsService [英] How to create a ContactsService using Google Contact API v3 with OAuth v2 UserCredentials

查看:175
本文介绍了如何使用带有OAuth v2 UserCredentials的Google Contact API v3创建ContactsService的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序正在将Google API Calendar V3与OAuth一起使用,效果很好.它将在第一时间征求用户的同意.使用日历服务可以轻松创建,修改和删除日历事件. 到目前为止,一切都很好!

My application is using Google API Calendar V3 with the OAuth and this works great. It will ask the user for their consent the first time. It is easy to use the Calendar Service to create, modify and delete calendar events. So far so good!

现在,我想询问用户添加和修改联系人数据的权限.通过添加以下字符串" https://www.google.com/m8/feeds/ "还要求用户批准联系人"访问权限. 这似乎有效. 但是,我找不到一种方法来根据通过上述过程收到的UserCredential创建ContactsService. 如何将UserCredential与ContactsService一起使用?

Now I want to ask the user permission for adding and modifying contact data. By adding the following string "https://www.google.com/m8/feeds/" the user is also asked for Contact access approval. This seems to work. However, I can’t find a way to create a ContactsService based on the UserCredential received through the above process. How do I use the UserCredential with a ContactsService?

我阅读了所有带有标签的问题:Google-contact和Google-api-dotnet-client. 我确实查看了API V3的文档,发现为Calendar,Drive和其他API创建了服务,但没有为Contact创建服务? 我想念什么?

I read all the questions with the tags: Google-contact and Google-api-dotnet-client. I did check the documentation on API V3 and I found service creation for Calendar, Drive and a bunch of other API’s but not for Contact? What do I miss?

这是我用于请求许可并启动日历服务的代码片段.

This is the code fragment I use to ask for permission and starting the Calendar Service.

using Google.Contacts;
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Calendar;
using Google.Apis.Calendar.v3;
using Google.Apis.Calendar.v3.Data;
using System.Threading;
using Google.Apis.Services;

namespace MY
{
class GoogleContact
{
    static public void start_service()
    {

        UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
            new ClientSecrets
            {
                ClientId = "clientID",
                ClientSecret = "clientsecret",
            },
            new[] { CalendarService.Scope.Calendar, "https://www.google.com/m8/feeds/" }, // This will ask the client for concent on calendar and contatcs
            "user",
            CancellationToken.None).Result;


        // Create the calendar service.
        CalendarService cal_service = new CalendarService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Calendar API Sample",
        });

        // How do I use the found credential to create a ContactsService????
        ContactsService service = new ContactsService("APP_NAME");


    }

我是否对我接下来要采取的步骤有任何反馈意见?

I appreciate any feedback on the next steps I have to take?

更新,在获得反馈后:

我添加了以下代码片段以使用联系人数据.

I added the following code fragment in order to use the Contact data.

        // Get the tokens from the FileDataStore
        var token = new FileDataStore("Google.Apis.Auth")
            .GetAsync<TokenResponse>("user"); 

        OAuth2Parameters parameters = new OAuth2Parameters
        {
            ClientId = mSecrets.ClientId,
            ClientSecret = mSecrets.ClientSecret,
            // Note: AccessToken is valid only for 60 minutes
            AccessToken = token.Result.AccessToken, 
            RefreshToken = token.Result.RefreshToken
        };

        RequestSettings settings = new RequestSettings(
            "Contact API Sample", parameters);
        ContactsRequest cr = new ContactsRequest(settings);
        Feed<Contact> f = cr.GetContacts();
        // The AccessCode is automatically updated after expiration!
        foreach (Contact c in f.Entries)
        {
            Console.WriteLine(c.Name.FullName);
        }

首先,我从FileDataStore中读取访问令牌和刷新令牌.
然后,我使用刚刚读取的令牌来设置OAuth2Parameters.
现在,我可以构造一个新的ContactsService和一个ContactsRequest.

First I read back the access token and the refresh token from the FileDataStore.
Then I set up the OAuth2Parameters using the token I just read.
And now, I can construct a new ContactsService and a ContactsRequest.

令我惊讶的是,ContactsService的访问令牌在到期后也会自动更新.

To my surprise the access token is automatically renewed after expiration also for the ContactsService.

推荐答案

您不能.
您正在尝试使两个不同的库(GData和Google API)一起工作.

You just can't.
You are trying to make two different libraries (GData and Google APIs) to work together.

GData文档位于: https://code.google.com/p/google -gdata/和Contact API的NuGet包可在以下位置获得: https ://www.nuget.org/packages/Google.GData.Contacts/.

GData documentation is available in: https://code.google.com/p/google-gdata/ and the NuGet package for the Contact API is available in: https://www.nuget.org/packages/Google.GData.Contacts/.

.NET的Google API客户端库是较新的库.但是,不幸的是,Contacts API不支持它. 以下列表中提供了所有受支持的Google API的列表: https://developers.google .com/api-client-library/dotnet/apis/,您可以找到

The Google APIs client library for .NET is the newer library. BUT, unfortunately the Contacts API doesn't support it. A list of all supported Google APIs is available in: https://developers.google.com/api-client-library/dotnet/apis/, you can find the Getting Started page there as well.

更新: 我不熟悉GData API,但是...

UPDATE: I'm not familiar with the GData API, but...

1)您可以在日历之外向AuhorizeAsync方法添加更多范围,以包括联系范围(如果有的话)

1) You can add more scopes besides calendar to the AuhorizeAsync method to include the contact scopes (if there are any)

2)您可以使用数据存储中返回的访问令牌+刷新令牌(默认情况下,您使用的是

2) You can use the returned access token + refresh token from the data store (By default you are using the FileDataStore, and initiate the GData request (again I'm not familiar with the API) to use the access token.

它可能对您有用,但是您需要进行更多调查..我没有尝试过,因为我对GData不熟悉.

It might work for you, but you have to investigate more.. I didn't try that, cause I'm not familiar with GData.

更新2:向FileDataStore添加正确的调用:

UPDATE 2: Adding the right call to FileDataStore:

var token = new FileDataStore("Google.Apis.Auth")
    .GetAsync<TokenResponse>("user");
var accessToken = token.AccessToken; // valid for 60 minutes ONLY.
var refreshToken = token.RefreshToken;

应检索包含访问和刷新令牌的令牌响应.

Should retrieve the token response that contains the access and refresh token.

** GoogleWebAuthorizationBroker 负责使用上述文件夹创建默认数据存储(如果用户不提供)(您的情况).

** GoogleWebAuthorizationBroker is responsible to create a default data store using the above folder if the user didn't provide one (your case).

** Auth库负责存储正确的数据.看看授权代码流以获取更多详细信息.

** The Auth library is responsible to store the right data. Take a look in the authorization code flow for more details.

这篇关于如何使用带有OAuth v2 UserCredentials的Google Contact API v3创建ContactsService的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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