我正在尝试使用google people api,但我遇到了很多困难。从api控制台生成的json文件不包含clientSecret。我在哪里可以得到它?所以我得到空授权代码。任何人都可以帮我解决这个问题吗?提前致谢
public void setUp() throws IOException {
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
// Go to the Google Developers Console, open your application's
// credentials page, and copy the client ID and client secret.
// Then paste them into the following code.
String clientId = "";
String clientSecret = "";
// Or your redirect URL for web based applications.
String redirectUrl = "urn:ietf:wg:oauth:2.0:oob";
String scope = "https://www.googleapis.com/auth/contacts.readonly";
// Step 1: Authorize -->
String authorizationUrl = new GoogleBrowserClientRequestUrl(clientId,
redirectUrl,
Arrays.asList(scope))
.build();
// Point or redirect your user to the authorizationUrl.
System.out.println("Go to the following link in your browser:");
System.out.println(authorizationUrl);
// Read the authorization code from the standard input stream.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is the authorization code?");
String code = in.readLine();
System.out.println(code);
// End of Step 1 <--
// Step 2: Exchange -->
GoogleTokenResponse tokenResponse = new GoogleAuthorizationCodeTokenRequest(
httpTransport, jsonFactory, clientId, clientSecret, code, redirectUrl).execute();
// End of Step 2 <--
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setClientSecrets(clientId, clientSecret)
.build()
.setFromTokenResponse(tokenResponse);
People peopleService = new People.Builder(httpTransport, jsonFactory, credential)
.build();
}
答案 0 :(得分:1)
要获取ClientID和ClientSecret,您应该创建Web应用程序ClientID,并且您需要它。
另一件值得注意的事情是response_type
生成的网址中的GoogleBrowserClientRequestUrl
将token
导致错误。我尝试将response_type
更改为code
,然后就可以了。
完整网址看起来像
https://accounts.google.com/o/oauth2/auth?client_id=YOUR_ANDROID_APPLICATION_CLIENT_ID&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=https://www.googleapis.com/auth/contacts.readonly
答案 1 :(得分:-1)
创建客户端ID时,选择“应用程序类型”作为“其他”,然后您可以找到clientSecret。 Screen shot