问题
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!
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?
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?
UPDATE, after getting feedback:
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);
}
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.
To my surprise the access token is automatically renewed after expiration also for the ContactsService.
回答1:
You just can't.
You are trying to make two different libraries (GData and Google APIs) to work together.
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/.
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.
UPDATE: I'm not familiar with the GData API, but...
1) You can add more scopes besides calendar to the AuhorizeAsync method to include the contact scopes (if there are any)
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.
It might work for you, but you have to investigate more.. I didn't try that, cause I'm not familiar with GData.
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 is responsible to create a default data store using the above folder if the user didn't provide one (your case).
** The Auth library is responsible to store the right data. Take a look in the authorization code flow for more details.
来源:https://stackoverflow.com/questions/27026829/how-to-create-a-contactsservice-using-google-contact-api-v3-with-oauth-v2-usercr