开发者

Using DotNetOpenAuth to get both Contacts & Calendars from a Google account together

I am writing a web application using ASP.NET MVC3 (C#), and I need to get the calendars and contacts from my logged-in user's Google account(s).开发者_如何学Go

Because I don't want to store the user's credentials, I am using OAuth (but not doing OpenID - I don't want my users to log in using their Google account).

I have the following code to get the access token and later use it to get the data:

var google = new WebConsumer(GoogleConsumer.ServiceDescription, this.TokenManager);

// Is Google calling back with authorization?
var accessTokenResponse = google.ProcessUserAuthorization();
if (accessTokenResponse != null) {
    this.AccessToken = accessTokenResponse.AccessToken;
    //SAVE ACCESS TOKEN TO DATABASE
} else if (this.AccessToken == null) {
    // If we don't yet have access, immediately request it.
    GoogleConsumer.RequestAuthorization(google, GoogleConsumer.Applications.Contacts);
}

Unfortunately, as you can see, I'm only requesting authorization for the Contacts, and the RequestAuthorization method doesn't allow me to set more than one application.

How can I do it without the need for several different tokens for the different scopes? (One for Calendar, one for Contacts, etc.)


The GoogleConsumer class is provided as source code to you in the DotNetOpenAuth.ApplicationBlock project. Please feel free to modify it to do just what you need.


Scopes can be placed in a string and separated by spaces

EDIT: Added from poster's comments

I use the following code when creating the scope string:

foreach (String s in Scopes) {
    sb.Append(String.Format("{0}", s));
}
scope = sb.ToString().Trim();

Where Scopes is an array of Strings (String[]) containing the scopes I want to use. When you request authorization from Google, then, you will see the list of services being requested displayed on the login screen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜