Oauth 2.0 如何设置在Java中调用Google People API的范围?
我正在使用以下代码。我有一个有效的accessToken。如何将范围设置为凭据上的电子邮件、个人资料?我正在使用OAuth2进行授权Oauth 2.0 如何设置在Java中调用Google People API的范围?,oauth-2.0,google-api,google-people-api,Oauth 2.0,Google Api,Google People Api,我正在使用以下代码。我有一个有效的accessToken。如何将范围设置为凭据上的电子邮件、个人资料?我正在使用OAuth2进行授权 static final String APPLICATION_NAME = "calendar-service"; static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); Credential c
static final String APPLICATION_NAME = "calendar-service";
static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
Credential credential = new
Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(accessToken);
// Build a new authorized API client service.
final NetHttpTransport HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
PeopleService service = new PeopleService.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
.setApplicationName(APPLICATION_NAME)
.build();
ListConnectionsResponse response = service.people().connections()
.list("people/me")
.setPersonFields("names,emailAddresses")
.execute();
List<Person> connections = response.getConnections();
if (connections != null && connections.size() > 0) {
for (Person person : connections) {
List<Name> names = person.getNames();
if (names != null && names.size() > 0) {
System.out.println("Name: " + person.getNames().get(0)
.getDisplayName());
} else {
System.out.println("No names available for connection.");
}
}
} else {
System.out.println("No connections found.");
}
return connections.get(0);
这是我的application.properties:
spring.security.oauth2.client.registration.google.clientId=XYZ456.apps.googleusercontent.com
spring.security.oauth2.client.registration.google.clientSecret=ABC123
spring.security.oauth2.client.registration.google.redirectUri={baseUrl}/oauth2/callback/{registrationId}
spring.security.oauth2.client.registration.google.scope=email,profile
请告知。有一个用于在Java中使用People API的方法
它包含以下行:
private static final List<String> SCOPES = Arrays.asList(PeopleServiceScopes.CONTACTS_READONLY);
private static final List SCOPES=Arrays.asList(PeopleServiceScopes.CONTACTS_READONLY);
这是一行,您可以根据需要将范围更改为其他范围,或添加其他范围
要了解您需要哪些作用域,请转到的文档页面
重要的
每次更改作用域时,都需要从计算机中删除令牌文件,以便触发新的授权流。感谢您提供的信息,特别是关于在我更改作用域时删除令牌文件的信息。我尝试删除令牌文件并重新运行应用程序,但没有任何运气。此外,快速入门适用于独立应用程序。我有一个ClientRegistration Spring OAuth2对象,知道上面有一个设置镜;但是,我不知道如何将其传递给PeopleService对象的Builder方法。我不使用Spring Boot,但是如果您向应用程序传递一个已经存在的访问令牌(该令牌是根据提供的作用域生成的),那么必须在创建该令牌之前给出作用域。你从哪里得到代币?看看是否有用。
private static final List<String> SCOPES = Arrays.asList(PeopleServiceScopes.CONTACTS_READONLY);