2011-03-23 118 views
7

根据此documentation,接收OAuth访问令牌的过程非常简单。我希望看到准备好接受OAuth 2.0访问令牌的所有可用API端点列表。但对于我目前的需求,我想以某种方式接收使用OAuth 2.0访问令牌的用户的usernameemail使用Google实验性OAuth 2.0访问现有API端点

我能够成功地接收,例如,从该端点数据:

https://www.google.com/m8/feeds/contacts/default/full 

但不能从该端点接收数据:

https://www.googleapis.com/userinfo/email 

我尝试都头基和查询字符串基传递单个访问令牌的方法。这里是一个头我想:

Authorization: OAuth My_ACCESS_TOKEN 

而且我甚至尝试的OAuth 1.0版本Authorization头的,但是......在OAuth 2.0用户,我们没有秘密访问令牌,例如。 Google在实施OAuth 2.0时使用持票人令牌,因此不需要额外的凭证。

任何人都使用Google OAuth 2.0成功接收了用户名和电子邮件?

+0

我遇到同样的问题。你有没有找到解决方案/解决方法? – ascandroli 2011-05-14 11:22:57

回答

1

我找到了答案我在寻找。我不得不PHP转换为MVC,但很容易:

http://codecri.me/case/430/get-a-users-google-email-address-via-oauth2-in-php/

我的MVC Login沙箱代码如下所示。 (使用JSON.Net http://json.codeplex.com/

public ActionResult Login() 
    { 
     string url = "https://accounts.google.com/o/oauth2/auth?"; 
     url += "client_id=<google-clientid>"; 
     url += "&redirect_uri=" + 
      // Development Server :P 
      HttpUtility.UrlEncode("http://localhost:61857/Account/OAuthVerify"); 
     url += "&scope="; 
     url += HttpUtility.UrlEncode("http://www.google.com/calendar/feeds/ "); 
     url += HttpUtility.UrlEncode("http://www.google.com/m8/feeds/ "); 
     url += HttpUtility.UrlEncode("http://docs.google.com/feeds/ "); 
     url += HttpUtility.UrlEncode("https://mail.google.com/mail/feed/atom "); 
     url += HttpUtility.UrlEncode("https://www.googleapis.com/auth/userinfo.email "); 
     url += HttpUtility.UrlEncode("https://www.googleapis.com/auth/userinfo.profile "); 
     url += "&response_type=code"; 

     return new RedirectResult(url); 
    } 

返回的code是从用户,然后需要被转成Authentication(的accessToken)来访问资源Authorization令牌的证明。 我的MVC OAuthVerify则看起来像:

public ActionResult AgentVerify(string code) 
    { 
     JObject json; 

     if (!string.IsNullOrWhiteSpace(code)) 
     { 
      NameValueCollection postData = new NameValueCollection(); 
      postData.Add("code", code); 
      postData.Add("client_id", "<google-clientid>"); 
      postData.Add("client_secret", "<google-client-secret>"); 
      postData.Add("redirect_uri", "http://localhost:61857/Account/OAuthVerify"); 
      postData.Add("grant_type", "authorization_code"); 

      try 
      { 
       json = JObject.Parse(
        HttpClient.PostUrl(
        new Uri("https://accounts.google.com/o/oauth2/token"), postData)); 
       string accessToken = json["access_token"].ToString(); 
       string refreshToken = json["refresh_token"].ToString(); 
       bool isBearer = 
        string.Compare(json["token_type"].ToString(), 
           "Bearer", 
           true, 
           CultureInfo.CurrentCulture) == 0; 

       if (isBearer) 
       { 
        json = JObject.Parse(
         HttpClient.GetUrl(
         new Uri("https://www.googleapis.com/oauth2/v1/userinfo?alt=json"), 
         accessToken)); 
        string userEmail = json["email"].ToString(); 
       } 
       return View("LoginGood"); 
      } 
      catch (Exception ex) 
      { 
       ErrorSignal.FromCurrentContext().Raise(ex); //ELMAH 
      } 
     } 
     return View("LoginBad"); 
    } 

要完成的一切是如何工作的,已经包含了我的情况下,任何人创建HttpClient的程序需要它。

public class HttpClient 
{ 
    public static string GetUrl(Uri url, string OAuth) 
    { 
     string result = string.Empty; 

     using (WebClient httpClient = new WebClient()) 
     { 
      httpClient.Headers.Add("Authorization","OAuth " + OAuth); 
      result = httpClient.DownloadString(url.AbsoluteUri); 
     } 

     return result; 
    } 

    public static string PostUrl(Uri url, NameValueCollection formData) 
    { 
     string result = string.Empty; 

     using (WebClient httpClient = new WebClient()) 
     { 
      byte[] bytes = httpClient.UploadValues(url.AbsoluteUri, "POST", formData); 
      result = Encoding.UTF8.GetString(bytes); 
     } 

     return result; 
    } 
} 

再次,这是测试代码只是为了让它正常工作,我不建议在生产环境中使用它。

相关问题