简体   繁体   English

使用Google登录名在wp8应用程序中对用户进行身份验证

[英]Authenticatioin of user in wp8 app using google login

I want to authenticate a user from my wp8 app using users google login credentials. 我想使用用户Google登录凭据从我的wp8应用中对用户进行身份验证。 So that I can get profile info of user. 这样我就可以获取用户的个人资料信息。 I found two articles in web with source code. 我在网上找到了两篇带有源代码的文章。 But I was unable to get what I want. 但是我无法获得想要的东西。

First code I've found in this Link . 我在此Link中找到的第一个代码。 But after getting authentication code it didn't have any code to get profile. 但是,在获取身份验证代码后,它没有任何代码可用于获取个人资料。 May be I could not understand. 可能是我听不懂。

Second code I've found in this Link . 我在此Link中找到的第二个代码。 It was following mvvm pattern, so I was totally blank to understand this code. 它遵循mvvm模式,因此我完全空白以了解此代码。

If anyone have used it properly, please help me. 如果有人正确使用了它,请帮助我。 What actually I want that after getting client id and client secret what to do in app to get user's profile info. 在获取客户端ID和客户端密码后,我实际上想要的是在应用程序中做什么以获取用户的个人资料信息。 Helps are appreciated. 帮助表示赞赏。 Thanks in advance. 提前致谢。

Here is code 这是代码

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);

}

} }

what to do after getting access token? 获取访问令牌后该怎么办?

This is the code that allows you to view the profile details: 这是允许您查看配置文件详细信息的代码:

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;
}

Just pass in the access_token you got from your prior code and the data should be contained in response.Data 只需传递您从先前代码中获得的access_token,数据就应该包含在response.Data

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM