如何使用Windows Phone 8.1 App中的代码获取Google API访问代码?

时间:2015-05-07 05:55:08

标签: windows-phone-8.1

我是Windows手机应用程序开发的新手。

我已在Google开发者控制台中创建了我的应用。 从我的Windows手机应用程序我正在使用" webview"为了呈现Google登录页面并成功登录,我得到了一个代码:4/akd........

任何人都可以告诉我如何使用"代码"来访问代码。第一次?

我尝试了以下方式:

public void GetProfileDetail(string code) 
{
    StringBuilder authLink = new StringBuilder();
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://accounts.google.com/o/oauth2/token");
    webRequest.ContentType = "application/x-www-form-urlencoded";
    webRequest.Method = "POST";

    authLink.AppendFormat("code={0}", code);
    authLink.AppendFormat("&client_id={0}", clientId);
    authLink.AppendFormat("&client_secret={0}", clientSecret);
    authLink.AppendFormat("&redirect_uri={0}", redirect_url);
    authLink.Append("&grant_type=authorization_code");

    UTF8Encoding utfenc = new UTF8Encoding();
    byte[] bytes = utfenc.GetBytes(authLink.ToString());
    Stream os = null;


    try // send the post
    {
        //webRequest.ContentLength = bytes.Length; // Count bytes to send

        os = webRequest.GetRequestStreamAsync().Result;
        os.Write(bytes, 0, bytes.Length);        // Send it
    }
    catch (Exception ex)
    {

    }
}

但它给了我一个错误。让我知道下一步该怎么做

提前致谢。

1 个答案:

答案 0 :(得分:0)

您正尝试通过网络视图进行OAuth身份验证,但这不是推荐的方式。

由于wp8.1有一个WebAuthenticationBroker类,您可以使用该类启动与提供商的OAuth流程(例如Google)。

可以在MSDN上找到一个很好的详细示例https://code.msdn.microsoft.com/windowsapps/Web-Authentication-d0485122

相关问题