0

我想使用用户谷歌登录凭据从我的 wp8 应用程序验证用户。这样我就可以获得用户的个人资料信息。我在网上找到了两篇带有源代码的文章。但我无法得到我想要的。

我在此链接中找到的第一个代码。但是在获得身份验证代码后,它没有任何代码来获取配置文件。可能是我无法理解。

我在此链接中找到的第二个代码。它遵循 mvvm 模式,所以我完全无法理解这段代码。

如果有人正确使用它,请帮助我。在获取客户端 ID 和客户端密码后,我实际上想要什么在应用程序中获取用户的个人资料信息。帮助表示赞赏。提前致谢。

这是代码

protected override void OnNavigatedTo(NavigationEventArgs e)

{

base.OnNavigatedTo(e);



IDictionary<string, string> parameters = this.NavigationContext.QueryString;



string authEndpoint = parameters["authEndpoint"];

string clientId = parameters["clientId"];

string scope = parameters["scope"];



string uri = string.Format("{0}?response_type=code&client_id={1}&redirect_uri={2}&scope={3}",

    authEndpoint,

    clientId,

    "urn:ietf:wg:oauth:2.0:oob",

    scope);



webBrowser.Navigate(new Uri(uri, UriKind.Absolute));

}

private async void LayoutRoot_Loaded(object sender, RoutedEventArgs e)

{

if(!App.loggedin)

{

    OAuthAuthorization authorization = new OAuthAuthorization(

    "https://accounts.google.com/o/oauth2/auth",

    "https://accounts.google.com/o/oauth2/token");

    TokenPair tokenPair = await authorization.Authorize(

        "YOUR_CLIENT_ID",

        "CLIENT_SECRET",

        new string[] { GoogleScopes.UserinfoEmail });



    // Request a new access token using the refresh token (when the access token was expired)

    TokenPair refreshTokenPair = await authorization.RefreshAccessToken(

        "YOUR_CLIENT_ID",

        "CLIENT_SECRET",

        tokenPair.RefreshToken);

}

}

获得访问令牌后该怎么办?

4

1 回答 1

1

这是允许您查看配置文件详细信息的代码:

private void LoadProfile(string access_token)
{
    Debug.WriteLine("loading profile");

    RestClient client = new RestClient("https://www.googleapis.com");
    client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(access_token);
    var request = new RestRequest("/oauth2/v1/userinfo", Method.GET);
    client.ExecuteAsync<Profile>(request, ProfileLoaded);
}
private void ProfileLoaded(IRestResponse<Profile> response)
{
    Profile = response.Data;
}

只需传入您从先前代码中获得的 access_token,数据应包含在response.Data

于 2015-06-26T12:38:54.923 回答