API контактов Google вызывает TypeError ('Получен неожиданный аргумент ключевого слова "% s"'% name) - PullRequest
0 голосов
/ 13 июля 2020

Я пробую свой проект Google API

from __future__ import print_function
import httplib2
from oauth2client import tools
from apiclient.discovery import build
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
from apiclient import discovery

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# Set up a Flow object to be used if we need to authenticate. This
# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
# the information it needs to authenticate. Note that it is called
# the Web Server Flow, but it can also handle the flow for
# installed applications.
#
# Go to the Google API Console, open your application's
# credentials page, and copy the client ID and client secret.
# Then paste them into the following code.
FLOW = OAuth2WebServerFlow(
    client_id='592262365202-h6qu4mrf2b043e2gv2qf5grjg0j53ádfasdfsf2s5.apps.googleusercontent.com',
    client_secret='VVxrBm9SEádfsf1ONzyX1oIEwSfB2',
    scope='https://www.googleapis.com/auth/contacts',
    user_agent='contact_cms/YOUR_APPLICATION_VERSION')

# If the Credentials don't exist or are invalid, run through the
# installed application flow. The Storage object will ensure that,
# if successful, the good Credentials will get written back to a
# file.

def get_credentials(FLOW):
    storage = Storage('info.dat')
    credentials = storage.get()
    if credentials is None or credentials.invalid == True:
        credentials = tools.run_flow(FLOW, storage)
    return credentials


# Create an httplib2.Http object to handle our HTTP requests and
# authorize it with our good Credentials.
def get_http(credentials):
    http = httplib2.Http()
    http = credentials.authorize(http)
    return http

# Build a service object for interacting with the API. To get an API key for
# your application, visit the Google API Console
# and look at your application's credentials page.
def print_list_google_contact_with_number_of_contacts(creds, numberofcontact):
    # Call the People API
    service = build('people', 'v1', credentials=creds)
    print('Ban dang in ra ', numberofcontact, ' danh ba')

    results = service.people().connections().list(
        resourceName='people/me',
        pageSize=numberofcontact,
        personFields='names,emailAddresses,phoneNumbers,emailAddresses,addresses').execute()
    connections = results.get('connections', [])

    # for person in connections:
    #     names = person.get('names', [])
    #     if names:
    #         name = names[0].get('displayName')
    #         print(name)
    for person in connections:
        names = person.get('names', [])
        if names:
            name = names[0].get('displayName')
        else:
            name = None
        phoneNumbers = person.get('phoneNumbers', [])
        if phoneNumbers:
            phoneNumber = phoneNumbers[0].get('value')
        else:
            phoneNumber = None
        Cities = person.get('addresses', [])
        if Cities:
            City = Cities[0].get('city')
        else:
            City = None
        emails = person.get('emailAddresses', [])
        if emails:
            email = emails[0].get('value')
        else:
            email = None
        print(f'{name}: {phoneNumber}: {City}: {email}')

def creat_a_google_contact(http):
    service = discovery.build('people', 'v1', http=http, discoveryServiceUrl='https://people.googleapis.com/$discovery/rest')
    service.people().createContact(parent='people/me', body={
        "names": [
            {
                'givenName': "Samkafafsafsafit"
            }
        ],
        "phoneNumbers": [
            {
                'value': "8600086024"
            }
        ],
        "emailAddresses": [
            {
                'value': "samkit5495@gmail.com"
            }
        ]
    }).execute()

def main():
    creds=get_credentials(FLOW)
    http=get_http(creds)
    printout=print_list_google_contact_with_number_of_contacts(creds,10)
    creat_a_google_contact(http)
    return print(printout)
if __name__ == '__main__':
    main()

Но когда я запускаю, веду журнал

Traceback (most recent call last):
  File "/Users/nguyenngoclinh/cms/cmsproject/google_contacts_cms/setup_your_app.py", line 116, in <module>
    main()
  File "/Users/nguyenngoclinh/cms/cmsproject/google_contacts_cms/setup_your_app.py", line 113, in main
    creat_a_google_contact(http)
  File "/Users/nguyenngoclinh/cms/cmsproject/google_contacts_cms/setup_your_app.py", line 104, in creat_a_google_contact
    'value': "samkit5495@gmail.com"

  File "/Users/nguyenngoclinh/.conda/envs/1z_vietnam/lib/python3.7/site-packages/googleapiclient/discovery.py", line 840, in method
    raise TypeError('Got an unexpected keyword argument "%s"' % name)
TypeError: Got an unexpected keyword argument "parent"

1 Ответ

2 голосов
/ 14 июля 2020

createContact() не имеет параметра parent. Он устарел:

https://developers.google.com/people/api/rest/v1/people/createContact

parent='people/me' устарело. Попробуй без этого.

...