2

SO 上的所有答案看起来都已过时,并且 Google 在 People API 文档中使用的 com.google.android.gms.plus.People 也已被弃用。附上下面的代码

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();
    ...
  }
4

1 回答 1

0

You should be able to get the authenticated user's profile by doing a get on "people/me" as in the example in https://developers.google.com/people/v1/requests#get-the-users-profile

You'll need these permissions: - profile - (Let's you use "people/me" to get the authenticated user) - https://www.googleapis.com/auth/user.birthday.read - (to get the user's birthday)

Right now for gender you will only get it if it's public. There's no scope to get private gender.

于 2017-06-21T01:28:37.153 回答