【问题标题】:How to use token obtained using GoogleAuthUtil class to get user information?如何使用通过 GoogleAuthUtil 类获得的令牌来获取用户信息?
【发布时间】:2015-08-18 17:11:47
【问题描述】:

我正在构建一个 Android 应用程序,我想在其中使用他们的 google 帐户对用户进行身份验证。我正在使用 GoogleAuthUtil 类从谷歌获取令牌,如下所示

protected String fetchToken() throws IOException{
    try {
        return GoogleAuthUtil.getToken(act, email, scope);
    } catch (GoogleAuthException e) {
        e.printStackTrace();
    }
    return null;
}

这里,act 是当前活动,email 是使用 accountpicker 和 scope = Audience:server:client_id:X 获得的值,其中 X 是 Web 应用程序的客户端 ID。

我得到了一些长结果,例如 eyJhbGciOiJSUzI1NiIsImtpZCI6ImFhMTkwMjZlYTgwNjYxNjI4ZjdiYzM5OTgyNDczZTFlYTE0NTVhNWQifQ.eyJpc3MiOiJhY2Nvd....... 作为 id 令牌,但我不知道如何使用此 ID 令牌从中检索用户信息。

请帮助我了解如何从 ID 令牌中获取用户信息。

【问题讨论】:

  • 谁能帮忙,我卡住了。

标签: java android oauth-2.0 google-oauth


【解决方案1】:

经过一番研究,我得到了关注。

ID token 用于验证用户的真实性,access token 用于获知用户即将登录的e-mail id 信息。 Access token 的获取方式与 id token 相同,如下所示。

 oAuthscopes = "oauth2:"+"https://www.googleapis.com/auth/userinfo.email"+" "+"https://www.googleapis.com/auth/plus.login";

protected String fetchAccessToken() throws IOException{
    try {
        return GoogleAuthUtil.getToken(act, email, oAuthscopes);
    }
    catch(GooglePlayServicesAvailabilityException e){
        act.handleException(e);
    }
    catch(UserRecoverableAuthException e){
        act.handleException(e);
    }
    catch (GoogleAuthException e) {
        e.printStackTrace();
    }
    return null;
}

注意 oAtuthScopes 的作用。您可以根据应用程序的需要指定任意数量的带空格的范围,以向用户请求权限。

现在,要从这些范围内获取用户信息,请执行以下操作。

URL url = new URL("https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token="+accessToken);
        StringBuffer val = returnJson(url);
url = new URL("https://www.googleapis.com/oauth2/v1/tokeninfo?alt=json&id_token="+idToken);
        StringBuffer valVerify = returnJson(url);

方法returnjSon(URL)是

StringBuffer returnJson(URL url) throws IOException {
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    InputStream iStream = conn.getInputStream();

    BufferedReader read = new BufferedReader(new InputStreamReader(iStream));

    String line;
    StringBuffer val = new StringBuffer();

    while((line = read.readLine())!=null){
        val.append(line);
    }

    return val;
}

访问令牌将返回类似,

{ "id": "10271045940xxxxxx", "email": "xxxx", "verified_email": true, "name": "XXXX", "given_name": "XXXX", “family_name”:“XXXX”,“链接”:“https://plus.google.com/+XXXX”, “图片”:“https://lh4.googleusercontent.com/xxxx”,“性别”:“男性”,“地区”:“XX”}

id 令牌会返回类似的东西。您可以在 logcat 中打印以进行预览。

然后,

        JSONObject reader = new JSONObject(val.toString());

        String email = (String)reader.get("email"));
        String name = (String)reader.get("name"));

        reader = new JSONObject(valVerify.toString());
        boolean verified = reader.getBoolean("email_verified"));

【讨论】:

    猜你喜欢
    • 2016-08-07
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多