windows phone 8.1[RT]应用程序中的谷歌集成

本文关键字:应用程序 谷歌 集成 RT phone windows | 更新日期: 2023-09-27 18:27:05

我正在使用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 的字符串

我想要帐户中的用户电子邮件和姓名以便注册或登录请帮我一下。感谢

windows phone 8.1[RT]应用程序中的谷歌集成

在类中实现函数间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
        }

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

我不是专家。

看起来你需要请求电子邮件范围:

https://developers.google.com/+/web/api/rest/oauth#登录范围

以及(可能)配置文件范围(同一页)。

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

我还在WIN RT应用程序中搜索了使用facebook和谷歌的登录,最终从这个链接找到了解决方案:Web身份验证代理示例

也许这对你也有帮助。