Java Code Examples for com.google.api.client.auth.oauth2.BearerToken#authorizationHeaderAccessMethod()

The following examples show how to use com.google.api.client.auth.oauth2.BearerToken#authorizationHeaderAccessMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AdManagerServiceClientFactoryHelperTest.java    From googleads-java-lib with Apache License 2.0 6 votes vote down vote up
@Test
public void testCheckServiceClientPreconditions_passOAuth2() throws Exception {
  AdManagerServiceClientFactoryHelper helper =
      new AdManagerServiceClientFactoryHelper(
          adsServiceClientFactory,
          adsServiceDescriptorFactory,
          soapClientHandler,
          adsLibConfiguration);

  Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod());

  AdManagerSession adManagerSession =
      new AdManagerSession.Builder()
          .withApplicationName("FooBar")
          .withNetworkCode("1000")
          .withEndpoint("https://ads.google.com")
          .withOAuth2Credential(credential)
          .build();

  helper.checkServiceClientPreconditions(
      adManagerSession,
      com.google.api.ads.admanager.lib.factory.helper.testing.v201911.TestService.class);
}
 
Example 2
Source File: AdManagerHttpHeaderHandlerTest.java    From googleads-java-lib with Apache License 2.0 6 votes vote down vote up
/**
 * Tests setting the headers.
 */
@SuppressWarnings("unchecked")
@Test
public void testSetHeaders() throws Exception {
  Object soapClient = new Object();
  Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod());

  AdManagerSession adManagerSession =
      new AdManagerSession.Builder()
          .withApplicationName("FooBar")
          .withOAuth2Credential(credential)
          .withEndpoint("https://ads.google.com")
          .withNetworkCode("networkCode")
          .build();

  adManagerHttpHeaderHandler.setHttpHeaders(soapClient, adManagerSession);

  verify(soapClientHandler).putAllHttpHeaders(eq(soapClient), any(Map.class));
}
 
Example 3
Source File: AdWordsServiceClientFactoryHelperTest.java    From googleads-java-lib with Apache License 2.0 6 votes vote down vote up
@Test
public void testCheckServiceClientPreconditions_passOAuth2() throws Exception {
  AdWordsServiceClientFactoryHelper helper = new AdWordsServiceClientFactoryHelper(
      adsServiceClientFactory, adsServiceDescriptorFactory, soapClientHandler,
      adsLibConfiguration);

  Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod());

  AdWordsSession adWordsSession = new AdWordsSession.Builder()
      .withUserAgent("FooBar")
      .withEndpoint("https://www.google.com")
      .withOAuth2Credential(credential)
      .withDeveloperToken("developerToken")
      .build();

  helper.checkServiceClientPreconditions(adWordsSession,
      com.google.api.ads.adwords.lib.factory.helper.testing.v201406.cm.TestService.class);
}
 
Example 4
Source File: AdWordsSessionTest.java    From googleads-java-lib with Apache License 2.0 6 votes vote down vote up
public AdWordsSessionTest(boolean isImmutable) {
  this.isImmutable = isImmutable;
  this.credential = new Credential(BearerToken.authorizationHeaderAccessMethod());

  this.reportingConfiguration =
      new ReportingConfiguration.Builder().skipReportHeader(true).skipReportSummary(true).build();

  this.allSettingsBuilder =
      new AdWordsSession.Builder()
          .withClientCustomerId("customer id")
          .withDeveloperToken("developer token")
          .withEndpoint("https://www.google.com")
          .enablePartialFailure()
          .enableValidateOnly()
          .withOAuth2Credential(credential)
          .withUserAgent("user agent")
          .withReportingConfiguration(reportingConfiguration);
}
 
Example 5
Source File: ComputeCredential.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * @param transport HTTP transport
 * @param jsonFactory JSON factory
 */
public Builder(HttpTransport transport, JsonFactory jsonFactory) {
  super(BearerToken.authorizationHeaderAccessMethod());
  setTransport(transport);
  setJsonFactory(jsonFactory);
  setTokenServerEncodedUrl(TOKEN_SERVER_ENCODED_URL);
}
 
Example 6
Source File: GoogleAuthorizationCodeFlow.java    From google-api-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * @param transport HTTP transport
 * @param jsonFactory JSON factory
 * @param clientSecrets Google client secrets
 * @param scopes collection of scopes to be joined by a space separator
 *
 * @since 1.15
 */
public Builder(HttpTransport transport, JsonFactory jsonFactory,
    GoogleClientSecrets clientSecrets, Collection<String> scopes) {
  super(BearerToken.authorizationHeaderAccessMethod(), transport, jsonFactory, new GenericUrl(
      GoogleOAuthConstants.TOKEN_SERVER_URL), new ClientParametersAuthentication(
      clientSecrets.getDetails().getClientId(), clientSecrets.getDetails().getClientSecret()),
      clientSecrets.getDetails().getClientId(), GoogleOAuthConstants.AUTHORIZATION_SERVER_URL);
  setScopes(scopes);
}
 
Example 7
Source File: AdManagerSessionTest.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
public AdManagerSessionTest(boolean isImmutable) {
  this.isImmutable = isImmutable;
  this.credential = new Credential(BearerToken.authorizationHeaderAccessMethod());
  this.allSettingsBuilder =
      new AdManagerSession.Builder()
          .withApplicationName("FooBar")
          .withEndpoint("https://ads.google.com")
          .withOAuth2Credential(credential)
          .withNetworkCode("networkCode");
}
 
Example 8
Source File: AuthorizationHeaderProviderTest.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAuthorizationHeader_oAuth2Refresh() throws Exception {
  final Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod());
  OAuth2Session adsSession = () -> credential;

  when(oAuth2AuthorizationHeaderProvider.getOAuth2AuthorizationHeader(
      (OAuth2Compatible) adsSession)).thenReturn("OAuth2 Header");
  when(adsLibConfiguration.isAutoRefreshOAuth2TokenEnabled()).thenReturn(true);

  assertEquals("OAuth2 Header",
      authorizationHeaderProvider.getAuthorizationHeader(adsSession, ENDPOINT_URL.toString()));

  verify(oAuth2Helper).refreshCredential(credential);
}
 
Example 9
Source File: AuthorizationHeaderProviderTest.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAuthorizationHeader_oAuth2NoRefresh() throws Exception {
  final Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod());
  OAuth2Session adsSession = () -> credential;

  when(oAuth2AuthorizationHeaderProvider.getOAuth2AuthorizationHeader(
      (OAuth2Compatible) adsSession)).thenReturn("OAuth2 Header");
  when(adsLibConfiguration.isAutoRefreshOAuth2TokenEnabled()).thenReturn(false);

  assertEquals("OAuth2 Header",
      authorizationHeaderProvider.getAuthorizationHeader(adsSession, ENDPOINT_URL.toString()));

  verify(oAuth2Helper, times(0)).refreshCredential(credential);
}
 
Example 10
Source File: AdWordsJaxWsHeaderHandlerTest.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetAuthenticationHeaders() throws Exception {
  Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod());
  Object soapClient = new Object();
  Map<String, Object> headerElements = new HashMap<String, Object>();
  AdWordsSession adWordsSession = new AdWordsSession.Builder().withOAuth2Credential(credential)
      .withDeveloperToken("developerToken").withUserAgent(USER_AGENT).build();

  headerHandler.setAuthenticationHeaders(soapClient, headerElements, adWordsSession);

  verify(authorizationHeaderHandler).setAuthorization(soapClient, adWordsSession);
}
 
Example 11
Source File: GoogleUtils.java    From mytracks with Apache License 2.0 4 votes vote down vote up
/**
 * Deletes Google Spreadsheets row.
 * 
 * @param context the context
 * @param accountName the account name
 * @param trackName the track name
 * @return true if deletion is success.
 */
public static boolean deleteSpreadsheetsRow(
    Context context, String accountName, String trackName) {
  try {
    // Get spreadsheet Id
    List<File> files = searchSpreadsheets(context, accountName);
    if (files == null || files.size() == 0) {
      return false;
    }
    String spreadsheetId = files.get(0).getId();

    // Get spreadsheet service
    SpreadsheetService spreadsheetService = new SpreadsheetService(
        "MyTracks-" + SystemUtils.getMyTracksVersion(context));
    Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod());
    credential.setAccessToken(
        SendToGoogleUtils.getToken(context, accountName, SendToGoogleUtils.SPREADSHEETS_SCOPE));
    spreadsheetService.setOAuth2Credentials(credential);

    // Get work sheet
    WorksheetFeed worksheetFeed = spreadsheetService.getFeed(new URL(
        String.format(Locale.US, SendSpreadsheetsAsyncTask.GET_WORKSHEETS_URI, spreadsheetId)),
        WorksheetFeed.class);
    Iterator<WorksheetEntry> worksheetEntryIterator = worksheetFeed.getEntries().iterator();
    while (worksheetEntryIterator.hasNext()) {
      WorksheetEntry worksheetEntry = (WorksheetEntry) worksheetEntryIterator.next();
      String worksheetTitle = worksheetEntry.getTitle().getPlainText();
      if (worksheetTitle.equals(SPREADSHEETS_WORKSHEET_NAME)) {
        URL url = worksheetEntry.getListFeedUrl();
        Iterator<ListEntry> listEntryIterator = spreadsheetService.getFeed(url, ListFeed.class)
            .getEntries().iterator();
        while (listEntryIterator.hasNext()) {
          ListEntry listEntry = (ListEntry) listEntryIterator.next();
          String name = listEntry.getCustomElements().getValue(SPREADSHEETS_TRANCK_NAME_COLUMN);
          if (name.equals(trackName)) {
            listEntry.delete();
            return true;
          }
        }
      }
    }
  } catch (Exception e) {
    Log.e(TAG, "Unable to delete spreadsheets row.", e);
  }
  return false;
}
 
Example 12
Source File: GoogleCredential.java    From google-api-java-client with Apache License 2.0 4 votes vote down vote up
public Builder() {
  super(BearerToken.authorizationHeaderAccessMethod());
  setTokenServerEncodedUrl(GoogleOAuthConstants.TOKEN_SERVER_URL);
}
 
Example 13
Source File: GoogleAuthorizationCodeFlow.java    From google-api-java-client with Apache License 2.0 3 votes vote down vote up
/**
 *
 * @param transport HTTP transport
 * @param jsonFactory JSON factory
 * @param clientId client identifier
 * @param clientSecret client secret
 * @param scopes collection of scopes to be joined by a space separator (or a single value
 *        containing multiple space-separated scopes)
 *
 * @since 1.15
 */
public Builder(HttpTransport transport, JsonFactory jsonFactory, String clientId,
    String clientSecret, Collection<String> scopes) {
  super(BearerToken.authorizationHeaderAccessMethod(), transport, jsonFactory, new GenericUrl(
      GoogleOAuthConstants.TOKEN_SERVER_URL), new ClientParametersAuthentication(
      clientId, clientSecret), clientId, GoogleOAuthConstants.AUTHORIZATION_SERVER_URL);
  setScopes(scopes);
}