/** * Generates a new offline credential and immediately refreshes it. * * @return a newly refreshed offline credential. * @throws OAuthException if the credential could not be refreshed. */ public Credential generateCredential() throws OAuthException { GoogleCredential credential = Strings.isNullOrEmpty(this.jsonKeyFilePath) ? generateCredentialFromClientSecrets() : generateCredentialFromKeyFile(); try { if (!oAuth2Helper.callRefreshToken(credential)) { throw new OAuthException( "Credential could not be refreshed. A newly generated refresh token or " + "secret key may be required."); } } catch (IOException e) { throw new OAuthException("Credential could not be refreshed.", e); } return credential; }
credential = getNewOAuth2Credential(); } catch (OAuthException e) { if (e.getMessage().contains("Connection reset")) { logger.info("Connection reset when getting Auth Token, retrying..."); credential = getNewOAuth2Credential();
/** * Generates a new offline credential and immediately refreshes it. * * @return a newly refreshed offline credential. * @throws OAuthException if the credential could not be refreshed. */ public Credential generateCredential() throws OAuthException { GoogleCredential credential = Strings.isNullOrEmpty(this.jsonKeyFilePath) ? generateCredentialFromClientSecrets() : generateCredentialFromKeyFile(); try { if (!oAuth2Helper.callRefreshToken(credential)) { throw new OAuthException( "Credential could not be refreshed. A newly generated refresh token or " + "secret key may be required."); } } catch (IOException e) { throw new OAuthException("Credential could not be refreshed.", e); } return credential; }
/** * Gets the OAuth2 header. * * @throws OAuthException if the OAuth2 token could not be refreshed. */ private String getOAuth2Header(OAuth2Compatible oAuth2Compatible) throws OAuthException { if (adsLibConfiguration.isAutoRefreshOAuth2TokenEnabled()) { try { oAuth2Helper.refreshCredential(oAuth2Compatible.getOAuth2Credential()); } catch (IOException e) { throw new OAuthException("OAuth2 token could not be refreshed.", e); } } return oAuth2AuthorizationHeaderProvider.getOAuth2AuthorizationHeader(oAuth2Compatible); } }
/** * Gets the OAuth2 header. * * @throws OAuthException if the OAuth2 token could not be refreshed. */ private String getOAuth2Header(OAuth2Compatible oAuth2Compatible) throws OAuthException { if (adsLibConfiguration.isAutoRefreshOAuth2TokenEnabled()) { try { oAuth2Helper.refreshCredential(oAuth2Compatible.getOAuth2Credential()); } catch (IOException e) { throw new OAuthException("OAuth2 token could not be refreshed.", e); } } return oAuth2AuthorizationHeaderProvider.getOAuth2AuthorizationHeader(oAuth2Compatible); } }
private Credential getNewOAuth2Credential() throws OAuthException { GoogleAuthorizationCodeFlow authorizationFlow = getAuthorizationFlow(); String authorizeUrl = authorizationFlow.newAuthorizationUrl().setRedirectUri(CALLBACK_URL).build(); System.out.println("\n**ACTION REQUIRED** Paste this url in your browser" + " and authenticate using your **AdWords Admin Account**: \n\n" + authorizeUrl + '\n'); // Wait for the authorization code. System.out.println("Type the code you received on the web page here: "); try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8))) { String authorizationCode = reader.readLine(); // Authorize the OAuth2 token. GoogleAuthorizationCodeTokenRequest tokenRequest = authorizationFlow.newTokenRequest(authorizationCode); tokenRequest.setRedirectUri(CALLBACK_URL); GoogleTokenResponse tokenResponse = tokenRequest.execute(); // Create the credential. Credential credential = new GoogleCredential.Builder() .setClientSecrets(clientId, clientSecret) .setJsonFactory(new JacksonFactory()) .setTransport(new NetHttpTransport()) .build() .setFromTokenResponse(tokenResponse); return credential; } catch (IOException e) { throw new OAuthException("An error occured obtaining the OAuth2Credential", e); } }
.build(); } catch (IOException e) { throw new OAuthException("Service account key file could not be loaded.", e);
.build(); } catch (IOException e) { throw new OAuthException("Service account key file could not be loaded.", e);