Exemple #1
0
  def testWithCachedRefreshToken(self):
    """Tests the entire workflow of making a request against AdWords.

    Uses a cached refresh token to generate a credential.

    Since this library is tightly integrated with SOAPpy, this test mocks out
    the HTTP level rather than the SOAPpy proxy level.
    """
    directory = self._CreateConfigPickles()

    try:
      with mock.patch(
          'oauth2client.client.OAuth2Credentials.refresh') as mock_refresh:
        client = AdWordsClient(path=directory)

        self.assertEquals(CLIENT_ID, client.oauth2credentials.client_id)
        self.assertEquals(CLIENT_SECRET, client.oauth2credentials.client_secret)
        self.assertEquals(REFRESH_TOKEN, client.oauth2credentials.refresh_token)

        def SetAccessToken(unused_http):
          client.oauth2credentials.access_token = ACCESS_TOKEN
        mock_refresh.side_effect = SetAccessToken

        campaign_service = self._CreateCampaignService(client)
        page = self._MakeSoapRequest(campaign_service)

        # Assert that the serialized objects returned in the SOAP response
        # deserialize to the expected output. Also check that we correctly
        # interpret the operations.
        self.assertEquals(EXPECTED_PAGE, page)
        self.assertEquals(8, client.GetOperations())
        client.oauth2credentials.refresh.assert_called_once_with(mock.ANY)
    finally:
      shutil.rmtree(directory)
from adspygoogle.adwords.AdWordsClient import AdWordsClient


# Initialize client object.
client = AdWordsClient(path=os.path.join('..', '..', '..', '..'))
client.use_mcc = True

# Initialize appropriate service.
info_service = client.GetInfoService(
    'https://adwords-sandbox.google.com', 'v201008')

# Construct info selector object and retrieve usage info.
today = datetime.datetime.today()
selector = {
    'dateRange': {
        'min': today.strftime('%Y%m01'),
        'max': today.strftime('%Y%m%d')
    },
    'apiUsageType': 'OPERATION_COUNT'
}
info = info_service.Get(selector)[0]

# Display results.
print ('The total number of operations consumed during \'%s\'-\'%s\' is \'%s\'.'
       % (selector['dateRange']['min'], selector['dateRange']['max'],
          info['cost']))

print
print ('Usage: %s units, %s operations' % (client.GetUnits(),
                                           client.GetOperations()))
# Import appropriate classes from the client library.
from adspygoogle.adwords.AdWordsClient import AdWordsClient

# Initialize client object.
client = AdWordsClient(path=os.path.join('..', '..', '..', '..'))
client.use_mcc = True

# Initialize appropriate service.
info_service = client.GetInfoService('https://adwords-sandbox.google.com',
                                     'v201003')

# Construct info selector object and retrieve usage info.
today = datetime.datetime.today()
selector = {
    'dateRange': {
        'min': today.strftime('%Y%m01'),
        'max': today.strftime('%Y%m%d')
    },
    'apiUsageType': 'OPERATION_COUNT'
}
info = info_service.Get(selector)[0]

# Display results.
print(
    'The total number of operations consumed during \'%s\'-\'%s\' is \'%s\'.' %
    (selector['dateRange']['min'], selector['dateRange']['max'], info['cost']))

print
print('Usage: %s units, %s operations' %
      (client.GetUnits(), client.GetOperations()))