开发者

MVC Implementation of OAuthConsumer in DotNetOpenAuth specially for Google Address Book

i have been searching on the net from last 2 days for MVC Implementation of OAuthConsumer Sample in DotNetOpenAuth, but still i did not found any solution. i had also tried to convert OAuthConsumer implementation from WebForms to MVC, but still unable to implement it cor开发者_如何学Gorectly. can anybody please help by referring some place to find a converter sample.


After 2 days of struggle I resolved the issue as follows, but I think it needs some more improvement.

private string AccessToken
{
    get { return (string)Session["GoogleAccessToken"]; }
    set { Session["GoogleAccessToken"] = value; }
}

private InMemoryTokenManager TokenManager
{
    get
    {
        var tokenManager = (InMemoryTokenManager)HttpContext.Application["GoogleTokenManager"];
        if (tokenManager == null)
        {
            string consumerKey = ConfigurationManager.AppSettings["GoogleOAuthConsumerKey"];
            string consumerSecret = ConfigurationManager.AppSettings["GoogleOAuthConsumerValue"];
            if (!string.IsNullOrEmpty(consumerKey))
            {
                tokenManager = new InMemoryTokenManager(consumerKey, consumerSecret);
                HttpContext.Application["GoogleTokenManager"] = tokenManager;
            }
        }

        return tokenManager;
    }
}


public ActionResult GoogleSync()
{
    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;
        XDocument contactsDocument = GoogleConsumer.GetContacts(google, this.AccessToken, 5, 1);
        var contactList = new List<GMailContact>();
        foreach (var entry in contactsDocument.Root.Elements(XName.Get("entry", "http://www.w3.org/2005/Atom")))
        {
            GMailContact newContact = new GMailContact { Name = string.Empty, Email = string.Empty };
            var titleElement = entry.Element(XName.Get("title", "http://www.w3.org/2005/Atom"));
            if (titleElement != null)
                newContact.Name = titleElement.Value;
            var emailElement = entry.Element(XName.Get("email", "http://schemas.google.com/g/2005"));
            if (emailElement != null && emailElement.Attribute("address") != null)
            {
                newContact.Email = emailElement.Attribute("address").Value;
            }

            contactList.Add(newContact);
        }

        ////var contacts = from entry in contactsDocument.Root.Elements(XName.Get("entry", "http://www.w3.org/2005/Atom"))
        ////               select new { Name = entry.Element(XName.Get("title", "http://www.w3.org/2005/Atom")).Value,
        ////                            Email = (XName.Get("email", "http://schemas.google.com/g/2005") == null ? "" : entry.Element(XName.Get("email", "http://schemas.google.com/g/2005")).Attribute("address").Value) };

        return View(contactList);
    }
    else if (this.AccessToken == null)
    {
        // If we don't yet have access, immediately request it.
        GoogleConsumer.RequestAuthorization(google, GoogleConsumer.Applications.Contacts);

        return this.Content("");
    }
    else
    {

        return this.Content("synchronization failed.");
    }

}


There isn't any MVC sample of an OAuth Consumer that I'm aware of. But since an OAuth consumer really has nothing to do with the presentation framework it shouldn't be any different between web forms and MVC. You should be able to just lift the consumer-related code directly out of the web forms sample and have it work in MVC.

If that doesn't work, please add more to your question that explains the problems you're seeing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜