且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用带有OAuth v2 UserCredentials的Google Contact API v3创建ContactsService

更新时间:2023-02-14 13:35:38

您不能.
您正在尝试使两个不同的库(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.