Google 集成在 windows phone 8.1[RT] 应用程序中

Google integation in windows phone 8.1[RT] application

我正在使用 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

的字符串

我需要帐户中的用户电子邮件和姓名以进行注册或登录 请帮助我。 谢谢

我不是专家。

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

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

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

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

我也在 WIN RT 应用程序中搜索了使用 facebook 和 google 登录,最后从这个 link 中找到了解决方案: Web authentication broker sample

也许它对你也有帮助。

为您的 class 实现 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 class 文件。 您将获得用户信息。