[英]I couldnt get jwt token from google
First of all i take client_id and client_secret from google developer tools.首先,我从谷歌开发者工具中获取 client_id 和 client_secret。 I am writing an API automation and I need to automatically get the Google token after signing in with a Google email and password in the option to sign in with Google on a site(eg: www.xyz.com ).
我正在编写一个 API 自动化程序,我需要在使用 Google 电子邮件和密码登录网站(例如: www.xyz.com )的选项中自动获取 Google 令牌。 First I added these dependencies to my pom.xml file
首先,我将这些依赖项添加到我的 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>
After i wrote these codes:在我写完这些代码之后:
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);
}
}
But Builder()
has protected access in com.google.auth.oauth2.GoogleCredentials.Builder
.但是
Builder()
在com.google.auth.oauth2.GoogleCredentials.Builder
中具有受保护的访问权限。 How can i access it or any advice for me.我怎样才能访问它或对我有什么建议。 Thank you for your advice
感谢您的建议
I've found another way that would probably suit you better:我找到了另一种可能更适合你的方法:
/** 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");
}
See that documentation for more information.有关详细信息,请参阅该文档。
But basically you should better store you clientId
and clientSecret
in a JSON file to have it not in the source code.但基本上,您最好将
clientId
和clientSecret
存储在一个 JSON 文件中,以免它出现在源代码中。
With that you can build your GoogleAuthorizationCodeFlow
object and receive from that the Credential
.有了它,您可以构建您的
GoogleAuthorizationCodeFlow
对象并从中接收Credential
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.