MVC在DotNetOpenAuth中实现OAuthConsumer专门用于Google通讯录

时间:2011-08-23 02:57:05

标签: dotnetopenauth

我一直在网上搜索过去2天在DotNetOpenAuth中使用OACConsumer Sample的MVC实现,但我仍未找到任何解决方案。 我还试图将OAuthConsumer实现从WebForms转换为MVC,但仍然无法正确实现它。 任何人都可以通过参考某个地方来寻找转换器样品。

2 个答案:

答案 0 :(得分:1)

经过2天的奋斗,我解决了以下问题,但我认为还需要进一步改进。

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.");
    }

}

答案 1 :(得分:0)

我不知道任何OAuth Consumer的MVC示例。但由于OAuth使用者实际上与表示框架无关,因此Web表单和MVC之间不应该有任何不同。您应该能够直接从Web表单示例中提取与使用者相关的代码,并使其在MVC中运行。

如果这不起作用,请在您的问题中添加更多内容,以解释您遇到的问题。