[英]I couldnt get jwt token from google
首先,我从谷歌开发者工具中获取 client_id 和 client_secret。 我正在编写一个 API 自动化程序,我需要在使用 Google 电子邮件和密码登录网站(例如: www.xyz.com )的选项中自动获取 Google 令牌。 首先,我将这些依赖项添加到我的 pom.xml 文件中
<dependency>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>1.12.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.api-client/google-api-client -->
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.30.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.apis/google-api-services-oauth2 -->
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-oauth2</artifactId>
<version>v2-rev157-1.25.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.api-client/google-api-client-jackson2 -->
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client-jackson2</artifactId>
<version>2.1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.oauth-client/google-oauth-client-jetty -->
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.30.6</version>
</dependency>
在我写完这些代码之后:
package JWTToken;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.auth.oauth2.GoogleCredentials;
public class JwtToken {
public static void main(String[] args) {
// Google OAuth 2.0
String clientId = "519214871295-o8********3m9i75ao7mm.apps.googleusercontent.com";
String clientSecret = "GOCSPX-*********7wKn5pXYZ";
// Google OAuth 2.0
GoogleCredentials credentials = new GoogleCredentials.Builder()
.setClientSecrets(clientId, clientSecret)
.setTransport(new NetHttpTransport())
.setJsonFactory(new JacksonFactory())
.build();
String accessToken = credentials.getAccessToken().getTokenValue();
System.out.println(accessToken);
}
}
但是Builder()
在com.google.auth.oauth2.GoogleCredentials.Builder
中具有受保护的访问权限。 我怎样才能访问它或对我有什么建议。 感谢您的建议
我找到了另一种可能更适合你的方法:
/** Authorizes the installed application to access user's protected data. */
private static Credential authorize() throws Exception {
// load client secrets
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
new InputStreamReader(CalendarSample.class.getResourceAsStream("/client_secrets.json")));
// set up authorization code flow
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, JSON_FACTORY, clientSecrets,
Collections.singleton(CalendarScopes.CALENDAR)).setDataStoreFactory(dataStoreFactory)
.build();
// authorize
return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user");
}
有关详细信息,请参阅该文档。
但基本上,您最好将clientId
和clientSecret
存储在一个 JSON 文件中,以免它出现在源代码中。
有了它,您可以构建您的GoogleAuthorizationCodeFlow
对象并从中接收Credential
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.