如何使用Android中的People API,Oauth 2 API获取谷歌登录用户的性别和生日信息?

时间:2017-06-13 12:01:44

标签: android oauth-2.0 google-people

所有关于SO的答案看起来都过时了,com.google.android.gms.plus.People也被弃用,Google在People API文档中使用了这些答案。附上以下代码

public void setUp() throws IOException {
    HttpTransport httpTransport = new NetHttpTransport();
    JacksonFactory jsonFactory = new JacksonFactory();


    String clientId = "YOUR_CLIENT_ID";
    String clientSecret = "YOUR_CLIENT_SECRET";

    String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
    String scope = "https://www.googleapis.com/auth/contacts.readonly";


    String authorizationUrl = new GoogleBrowserClientRequestUrl(clientId,
                                                                redirectUrl,
                                                                Arrays.asList(scope))
        .build();

    System.out.println("Go to the following link in your browser:");
    System.out.println(authorizationUrl);


    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("What is the authorization code?");
    String code = in.readLine();

    GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
        httpTransport, jsonFactory, clientId, clientSecret, code, redirectUrl).execute();


    GoogleCredential credential = new GoogleCredential.Builder()
        .setTransport(httpTransport)
        .setJsonFactory(jsonFactory)
        .setClientSecrets(clientId, clientSecret)
        .build()
        .setFromTokenResponse(tokenResponse);

    People peopleService = new People.Builder(httpTransport, jsonFactory, credential)
        .build();
    ...
  }

1 个答案:

答案 0 :(得分:0)

您应该能够通过“{/ 3}}

中的示例”获取“人/我”来获取经过身份验证的用户的个人资料

您需要这些权限:   - 个人资料 - (让我们使用“people / me”来获取经过身份验证的用户)   - https://developers.google.com/people/v1/requests#get-the-users-profile - (为了获得用户的生日)

现在,对于性别而言,只有在公开的情况下才能获得。没有获得私人性别的余地。