【问题标题】:Google integation in windows phone 8.1[RT] applicationWindows phone 8.1[RT] 应用程序中的 Google 集成
【发布时间】:2016-03-23 14:32:32
【问题描述】:

我正在使用 windows phone 8.1 应用程序,我想使用 Google Plus 登录,为此我从 Google 创建了客户端 ID 和客户端密码

 string uri = string.Format("{0}?response_type=code&client_id={1}&redirect_uri={2}&scope={3}&approval_prompt=force",
            authEndpoint,
            clientId,
            "urn:ietf:wg:oauth:2.0:oob",
            scope);
 webBrowser.Navigate(new Uri(uri, UriKind.Absolute));

我得到了类似4/BGIl1Na3TQJlAD8SQ7blvHyONJ_Jyav8COHa7tIrAdo的字符串

我想注册或登录帐户中的用户电子邮件和名称 请帮助我。 谢谢你

【问题讨论】:

    标签: c# oauth-2.0 windows-phone-8.1 google-oauth


    【解决方案1】:

    我不是专家。

    您似乎需要请求电子邮件范围:

    https://developers.google.com/+/web/api/rest/oauth#login-scopes

    以及(可能)个人资料范围(同一页面)。

    如果这没有帮助,请编辑您的问题并显示更多代码(隐藏您的 API 密钥等)。

    【讨论】:

      【解决方案2】:

      我还在 WIN RT Apps 中搜索了使用 facebook 和 google 登录,最后从这个链接找到了解决方案: Web authentication broker sample

      也许它也会帮助你。

      【讨论】:

      • 感谢您回答 saurabh ,但它只会对用户进行身份验证。我想要身份验证后的用户详细信息
      【解决方案3】:

      实现接口 IWebAuthenticationContinuable 到你的类,然后在 ContinueWebAuthentication() 方法下使用这个代码:

      var authData = GetGoogleSuccessCode(args.WebAuthenticationResult.ResponseData); 
      var userData = await GetTokenAndUserInfo(authData);
      
      private string GetGoogleSuccessCode(string data)
              {
                  if (string.IsNullOrEmpty(data)) return null;
                  var parts = data.Split('&')[0].Split('=');
                  for (int i = 0; i < parts.Length; ++i)
                  {
                      if (parts[i] == "Success code")
                      {
                          return parts[i + 1];
                      }
                  }
                  return null;
              }
      
      public async Task<string> GetTokenAndUserInfo(string code)
              {
                  var client = new HttpClient();
                  var auth = await client.PostAsync("https://accounts.google.com/o/oauth2/token", new FormUrlEncodedContent(new[]
                      {
                          new KeyValuePair<string, string>("code", code),
                          new KeyValuePair<string, string>("client_id",Constants.GoogleAppId), 
                          new KeyValuePair<string, string>("client_secret",Constants.GoogleAppSecret), 
                          new KeyValuePair<string, string>("grant_type","authorization_code"),
                          new KeyValuePair<string, string>("redirect_uri","urn:ietf:wg:oauth:2.0:oob"),  
                      }));
      
                  var data = await auth.Content.ReadAsStringAsync();
                  var j = JToken.Parse(data);
                  var token = j.SelectToken("access_token");
                  var searchUrl = "https://www.googleapis.com/oauth2/v2/userinfo";
                  client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token.ToString());
                  HttpResponseMessage res = await client.GetAsync(searchUrl);
                  string content = await res.Content.ReadAsStringAsync();
                  return content;
              }
      

      并在 App.xaml.cs 文件中添加以下代码:

      public static ContinuationManager ContinuationManager { get; private set; }
      
      
       public App()
      {
          ContinuationManager = new ContinuationManager();
      }
      
      private void OnSuspending(object sender, SuspendingEventArgs e)
              {
                  var deferral = e.SuspendingOperation.GetDeferral();
      #if WINDOWS_PHONE_APP
                  ContinuationManager.MarkAsStale();
      #endif
                  // TODO: Save application state and stop any background activity
                  deferral.Complete();
              }
      
      protected override void OnActivated(IActivatedEventArgs args)
              {
      #if WINDOWS_PHONE_APP
                  if (args.Kind == ActivationKind.WebAuthenticationBrokerContinuation)
                  {
                      var continuationEventArgs = args as IContinuationActivatedEventArgs;
                      if (continuationEventArgs != null)
                      {
                          ContinuationManager.Continue(continuationEventArgs);
                          ContinuationManager.MarkAsStale();
                      }
      
                  }
      #endif
              }
      

      不要忘记在您的项目中包含 Continuation Manager 类文件。 您将获得用户信息。

      【讨论】:

      • 你好 Saurabh,谢谢你的回答。我已经为 access_token 尝试了很多,但它对我有用,你能有任何参考或演示,我可以找到我错的地方
      • 抱歉,我没有收到您的问题。您是否没有为您的应用程序获取 access_token 或者您正面临其他问题?能详细解释一下问题吗
      • 我从 args.WebAuthenticationResult.ResponseData 得到 :: accounts.google.com/o/oauth2/… ",我从 GetGoogleSuccessCode(args.WebAuthenticationResult.ResponseData) 得到 null,因为没有成功代码
      • 你是从 GetTokenAndUserInfo() 方法得到的??
      • 我是从 args.WebAuthenticationResult.ResponseData , ContinueWebAuthentication() 方法得到的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 1970-01-01
      • 2014-11-04
      相关资源
      最近更新 更多