簡體   English   中英

我無法從谷歌獲得 jwt 令牌

[英]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");
}

有關詳細信息,請參閱該文檔。

但基本上,您最好將clientIdclientSecret存儲在一個 JSON 文件中,以免它出現在源代碼中。

有了它,您可以構建您的GoogleAuthorizationCodeFlow對象並從中接收Credential

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM