2017-06-13 53 views
3

SO上的所有答案都看起來過時了,com.google.android.gms.plus.People也不贊成Google在People API文檔中使用的答案。附加以下如何在Android中使用People API,Oauth 2 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(); 
    ... 
    } 

回答

相關問題