コード例 #1
0
def get_auth_token():
    scope = 'https://www.googleapis.com/auth/contacts.readonly'
    user_agent = __name__
    client_secrets = os.path.join(os.path.dirname(__file__),
                                  CLIENT_SECRETS_JSON)
    filename = os.path.splitext(__file__)[0] + '.dat'

    flow = client.flow_from_clientsecrets(
        client_secrets,
        scope=scope,
        message=tools.message_if_missing(client_secrets))

    storage = file.Storage(filename)
    credentials = storage.get()
    if credentials is None or credentials.invalid:
        if args.non_interactive:
            sys.stderr.write(
                'ERROR: Invalid or missing Oauth2 credentials. To reset auth flow manually, run without --non_interactive\n'
            )
            sys.exit(1)
        else:
            credentials = tools.run_flow(flow, storage, args)

    j = json.loads(open(filename).read())

    return gdata.gauth.OAuth2Token(j['client_id'],
                                   j['client_secret'],
                                   scope,
                                   user_agent,
                                   access_token=j['access_token'],
                                   refresh_token=j['refresh_token'])
コード例 #2
0
def get_auth_token():
    scope = 'https://www.googleapis.com/auth/contacts.readonly'
    user_agent = __name__
    client_secrets = os.path.join(os.path.dirname(__file__), CLIENT_SECRETS_JSON)
    filename = os.path.splitext(__file__)[0] + '.dat'

    flow = client.flow_from_clientsecrets(client_secrets, scope=scope, message=tools.message_if_missing(client_secrets))
    
    storage = file.Storage(filename)
    credentials = storage.get()
    if credentials is None or credentials.invalid:
        if args.non_interactive:
            sys.stderr.write('ERROR: Invalid or missing Oauth2 credentials. To reset auth flow manually, run without --non_interactive\n')
            sys.exit(1)
        else:
            credentials = tools.run_flow(flow, storage, args)

    j = json.loads(open(filename).read())

    return gdata.gauth.OAuth2Token(j['client_id'], j['client_secret'], scope, user_agent, access_token = j['access_token'], refresh_token = j['refresh_token'])
コード例 #3
0
def create_authorized_gdclient():
    baseDir = xdg.BaseDirectory.save_config_path('nprstuff')
    credPath = os.path.join(baseDir, 'credentials.json')
    storage = oauth2client.file.Storage(credPath)
    if os.path.isfile(credPath):  # file exists
        credentials = storage.get()
        credentials.refresh(httplib2.Http())
        if not credentials.access_token_expired:
            return create_authorized_gdclient_credential(credentials)

    password = raw_input('Enter the password: '******'https://tanimislam.ddns.net/flask/api/google/pythonclient',
        auth=('*****@*****.**', password))
    data = response.json()
    print(data)
    fd, jsonfile = tempfile.mkstemp(suffix='.json')
    json.dump(data, open(jsonfile, 'w'))
    flow = client.flow_from_clientsecrets(
        jsonfile,
        scope='https://www.googleapis.com/auth/contacts.readonly',
        redirect_uri='urn:ietf:wg:oauth:2.0:oob')
    os.remove(jsonfile)
    flow.params['include_granted_scopes'] = 'true'
    flow.params['access_type'] = 'offline'
    #
    #client_id = data['client_id']
    #client_secret = data['client_secret']
    #scope = 'https://www.googleapis.com/auth/contacts.readonly'
    #application_redirect_uri = data['redirect_uris'][0]
    #auth_token = gdata.gauth.OAuth2Token( client_id = client_id, client_secret = client_secret,
    #                                      scope = scope, user_agent = 'dummy-sample' )
    #authorize_url = auth_token.generate_authorize_url(redirect_uri=application_redirect_uri)
    authorize_url = flow.step1_get_authorize_url()
    webbrowser.open(authorize_url)
    auth_code = raw_input('Enter the auth code: ')
    credentials = flow.step2_exchange(auth_code)
    storage.put(credentials)
    return create_authorized_gdclient_credential(credentials)
コード例 #4
0
def create_authorized_gdclient():
    baseDir = xdg.BaseDirectory.save_config_path( 'nprstuff' )
    credPath = os.path.join( baseDir, 'credentials.json' )    
    storage = oauth2client.file.Storage( credPath )
    if os.path.isfile( credPath ): # file exists
        credentials = storage.get()
        credentials.refresh( httplib2.Http() )
        if not credentials.access_token_expired:
            return create_authorized_gdclient_credential( credentials )

    password = raw_input('Enter the password: '******'https://tanimislam.ddns.net/flask/api/google/pythonclient',
                             auth = ( '*****@*****.**', password ) )
    data = response.json()
    print(data)
    fd, jsonfile = tempfile.mkstemp( suffix = '.json' )
    json.dump( data, open( jsonfile, 'w' ) )
    flow = client.flow_from_clientsecrets(
        jsonfile,
        scope = 'https://www.googleapis.com/auth/contacts.readonly',
        redirect_uri = 'urn:ietf:wg:oauth:2.0:oob')
    os.remove( jsonfile )
    flow.params['include_granted_scopes'] = 'true'
    flow.params['access_type'] = 'offline'
    #
    #client_id = data['client_id']
    #client_secret = data['client_secret']
    #scope = 'https://www.googleapis.com/auth/contacts.readonly'
    #application_redirect_uri = data['redirect_uris'][0]
    #auth_token = gdata.gauth.OAuth2Token( client_id = client_id, client_secret = client_secret,
    #                                      scope = scope, user_agent = 'dummy-sample' )
    #authorize_url = auth_token.generate_authorize_url(redirect_uri=application_redirect_uri)
    authorize_url = flow.step1_get_authorize_url()
    webbrowser.open( authorize_url )
    auth_code = raw_input('Enter the auth code: ')
    credentials = flow.step2_exchange( auth_code )
    storage.put( credentials )
    return create_authorized_gdclient_credential( credentials )
コード例 #5
0
from oauth2client import tools, client
from oauth2client.file import Storage

parser = argparse.ArgumentParser(parents=[tools.argparser])
parser.add_argument('args', nargs=argparse.REMAINDER)
flags = parser.parse_args()
flags.noauth_local_webserver = True

store = Storage("/tmp/cred")

credentials = store.get()

if not credentials or credentials.invalid:
    flow = client.flow_from_clientsecrets(
        "/home/prius/Downloads/client_id.json",
        ["https://www.google.com/m8/feeds/"],
    )

    credentials = tools.run_flow(flow, store, flags)

auth2token = gdata.gauth.OAuth2TokenFromCredentials(credentials)
gd_client = gdata.contacts.client.ContactsClient(source='contacts-to-latin')
gd_client = auth2token.authorize(gd_client)

feed = gd_client.GetContacts(max_results=999999)

for contact in feed.entry:
    contact_changed = False
    if contact.name is not None:
        if "additional_name" in contact.name.__dict__ and \
            contact.name.additional_name is not None: