'How to run google people service build in server to make token.pickle

I try to make micro sever to connect google people API. I do in my local host 'MACOS', everything oke. But after i deploy to server 'CENTOS', server can not render the auth url: code to make google service build

def contact_service_build(pickle_file_name):
    """Shows basic usage of the People API.
    Prints the name of the first 10 connections.
    """
    path = str(dir.dir)
    token_file = path + '/project/google_auth/'+pickle_file_name+ '.pickle'
    secrets_file = path + '/project/credentials/credentials.json'
    SCOPES = ['https://www.googleapis.com/auth/contacts']
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists(token_file):
        with open(token_file, 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        print('not creds or not creds.valid')
        if creds and creds.expired and creds.refresh_token:
            print('creds and creds.expired and creds.refresh_token')
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(secrets_file, SCOPES)
            creds = flow.run_local_server(port=0)
            print('creds = flow.run_local_server(port=0)')
        # Save the credentials for the next run
        with open(token_file, 'wb') as token:
            pickle.dump(creds, token)
    service = build('people', 'v1', credentials=creds)
    return service

code to make google contact

def creat_google_contact(service, first_name, last_name, phone, email, streetAddress, extendedAddress, city, zip,
                         province,
                         country, country_code, biographies, company):
    # POST / v1 / people: createContact
    # HTTP / 1.1
    # Body: {"names": [{"givenName": "John", "familyName": "Doe"}]}
    # Host: people.googleapis.comcompany
    # service = discovery.build('people', 'v1', http=http, discoveryServiceUrl='https://people.googleapis.com/$discovery/rest')
    try:
        service.people().createContact(body={
            "organizations": [
                {
                    "name": str(company)
                }
            ],
            "biographies": [
                {
                    "value": str(biographies),
                    "contentType": 'TEXT_HTML'
                }
            ],
            "names": [
                {
                    "givenName": str(last_name),
                    "familyName": str(first_name)
                }
            ],
            "phoneNumbers": [
                {
                    'value': str(phone)
                }
            ],
            "emailAddresses": [
                {
                    'value': str(email)
                }
            ],
            "addresses": [
                {
                    "streetAddress": str(streetAddress),
                    "extendedAddress": str(extendedAddress),
                    "city": str(city),
                    "region": str(province),
                    "postalCode": str(zip),
                    "country": str(country),
                    "countryCode": str(country_code)
                }
            ]
        }).execute()
        print("da ghi lenh danh ba gooogle", first_name, last_name)
    except Exception as e:
        print('khong ghi duoc danh ba',first_name,last_name)
        pass

in the SSH Terminal i can see python print out

127.0.0.1 - - [09/Sep/2020 12:04:30] "GET /sync_contact HTTP/1.0" 200 -
not creds or not creds.valid
Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=44359f0pm.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A36883%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fcontacts&state=7mjA48LHGRZnRT&access_type=offline

But i dont know the way to open this link in web brower????



Solution 1:[1]

Thank for all After studied, i found the way to solution

@contact.route('/sync_authorize')
@login_required
def sync_authorize():
    SCOPES = 'https://www.googleapis.com/auth/userinfo.profile openid https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/contacts'
      # Create flow instance to manage the OAuth 2.0 Authorization Grant Flow steps.
    path = str(dir.dir)
    secrets_file = path + '/project/credentials/credentials.json'
    flow = Flow.from_client_secrets_file(
          secrets_file, scopes=SCOPES)

    flow.redirect_uri = url_for('contact.sync_callback', _external=True)

    authorization_url, state = flow.authorization_url(
          access_type='offline',
          include_granted_scopes='true')

    flask.session['state'] = state

    return redirect(authorization_url)


@contact.route('/sync_callback')
@login_required
def sync_callback():
    email = current_user.email
    name = email.split('@')[0]
    name = name.split('.')[0]
    path = str(dir.dir)
    token_file = path + '/project/google_auth/' + name + '.pickle'
    print('token_file: ',token_file)
    SCOPES = 'https://www.googleapis.com/auth/userinfo.profile openid https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/contacts'
    state = flask.session['state']
    secrets_file = path + '/project/credentials/credentials.json'

    credentials = None

    if os.path.exists(token_file):
        with open(token_file, 'rb') as token:
            credentials = pickle.load(token)
    else:
        flow = Flow.from_client_secrets_file(
            secrets_file, scopes=SCOPES, state=state)
        flow.redirect_uri = url_for('contact.sync_callback', _external=True)
        # Use the authorization server's response to fetch the OAuth 2.0 tokens.
        authorization_response = request.url
        print('authorization_response',authorization_response)
        flow.fetch_token(authorization_response=authorization_response)
        credentials = flow.credentials
        with open(token_file, 'wb') as token:
            pickle.dump(credentials, token)
    service = build('people', 'v1', credentials=credentials)
-------finish build service-------
CODE TO CREAT DELETE CONTACTS

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Linh Nguy?n Ng?c