I have Done the authentication and after that
I am trying to download the file from the DFA
//////////////////////Code//////////////////////////////
import httplib2
import os
import sys
import time
import urllib
import argparse
import pprint
import httplib
sys.path.insert(0, os.path.join('MY_dfa_api_auth.pkl_Location', 'MY_dfa_api_config.pkL_LOCATION', '..', '..'))
from adspygoogle import DfaClient
from oauth2client.client import FlowExchangeError
from oauth2client.client import OAuth2WebServerFlow
from apiclient import sample_tools
from oauth2client import client
from apiclient.discovery import build
DFA_USER_PROFILE_NAME ='DFA_USER_NAME'
# Visit https://code.google.com/apis/console to generate your client_id,
# client_secret and to register your redirect_uri.
# See the oauth2client wiki for more information on performing the OAuth2 flow:
# http://code.google.com/p/google-api-python-client/wiki/OAuth2
OAUTH2_CLIENT_ID = 'MyClientID'
OAUTH2_CLIENT_SECRET = 'MySecretkey '
dimension={}
report_id=REPORTID
profile_id=PROFILEID
file_id=FILEID
def main():
# We're using the oauth2client library:
scopeList = ["https://www.googleapis.com/auth/devstorage.read_only","https://www.googleapis.com/auth/dfareporting"]
# http://code.google.com/p/google-api-python-client/downloads/list
flow = OAuth2WebServerFlow(
client_id=OAUTH2_CLIENT_ID,
client_secret=OAUTH2_CLIENT_SECRET,
scope=scopeList,
redirect_uri='urn:ietf:wg:oauth:2.0:oob',
user_agent='SampleAPI')
# Get the authorization URL to direct the user to.
authorize_url = flow.step1_get_authorize_url()
print ('Log in to your Google Account and open the following URL: \n%s\n' %authorize_url)
print 'After approving the token enter the verification code (if specified).'
code = raw_input('Code: ').strip()
global credential
credential=None
try:
credential = flow.step2_exchange(code)
except FlowExchangeError as e:
sys.exit('Authentication has failed: %s' % e)
# Create the DfpClient and set the OAuth2 credentials.
client = DfaClient(headers={
'Username': DFA_USER_PROFILE_NAME,
'oauth2credentials': credential
})
# OAuth2 credentials objects can be reused
credentials = client.oauth2credentials
client.oauth2credentials = credentials
http = httplib2.Http()
print 'OAuth2 authorization successful!'
try :
service = build('dfareporting', 'v1.3', http=http)
report_file = service.reports().files().get(profileId=profile_id, reportId=report_id, fileId=file_id).execute()
download_url = report_file['urls']['apiUrl']
response, content = http.request(download_url)
if response['status'] == '200':
print content
except BaseException as e :
print "Error in ",e
if __name__ == '__main__':
main()
/////////////////////////////////////////////////////////
/////////////////////On the CONSOLE////////////////////////////
//////////////////////////////////////////////////////////////
Log in to your Google Account and open the following URL:
https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdevstorage.read_only+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdfareporting&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&client_id="Client ID"&access_type=offline
After approving the token enter the verification code (if specified).
Code:I have enter the Oauth Code !!!
OAuth2 authorization successful!
HttpError 401 when requesting https://www.googleapis.com/dfareporting/v1.3/Profile_ID/reports/Report_ID/files/File_ID?alt=json returned "Login Required">
//////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////
Please help me where I am going wrong with the code and the authentication is failing ??