I want to use gmail API in google collateral, but there is an error during authentication.

Asked 2 years ago, Updated 2 years ago, 106 views

I wanted to use the gmail API in Google collaboration, so I did the following, but I got an error during authentication, so could you tell me what to do?

1. Create an oauth 2.0 client ID (desktop client) on the google cloud platform and save the json file to the google drive mounted on the collateral.

2. Run Python code by copying almost complete code samples from Steps 1 and 2 of the following sites
https://developers.google.com/gmail/api/quickstart/python?hl=ja

3. For authentication, we did the following:
·Please visit this URL to authorize this application: https://... and the link on the authentication screen appears.
"·Select an account at the link,
·This application has not been confirmed by Google.Access to the application currently being tested has been granted.US>Continue only if you trust the developer you are inviting.
"·Select ""Accessible Information"" on the ""~ is requesting access to Google Account"" screen

"

4.Error
このCannot access this site, localhost denied connection...ERR_CONNECTION_REFUSED 」

I tried to summarize it as much as possible, but I apologize for the long sentence.Thank you for your cooperation.

Thank you for your comment.The code is as follows:

 pip install --upgrade google-api-python-client google-auth-httplib2google-auth-oauthlib

from__future_import print_function
importos.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials

from email.mime.text import MIMEText
import base64


# If modifying these scopes, delete the file token.json.
SCOPES= ['https://www.googleapis.com/auth/gmail.send']

create_message(sender, to, subject, message_text):
    message = MIMEText(message_text)
    message ['to'] = to
    message ['from'] = sender
    message ['subject'] = subject
    return {'raw':base64.urlsafe_b64encode(message.as_string().encode()) .decode}

def send_message(service, user_id, message):
    try:
        message=(service.users().messages().send(userId=user_id, body=message)
                    .execute())
        print('Message Id: %s'%message['id'])
        return message
    except errors.HttpError as:
        print('An error occurred: %s'% error)

defmain():
    "Shows basic usage of the Gmail API.
    Lists the user's Gmail labels.
    """
    credits = None
    # The file token.json stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow complements for the first
    # time
    ifos.path.exists('token.json'):
        credits = Credentials.from_authorized_user_file('token.json', SCOPES)
    # If there are no (valid) credentials available, let the user log in.
    if not cred or not cred.valid:
        if credits and credits.expired and credits.refresh_token:
            credits.refresh (Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                '/content/drive/MyDrive/****/****.json', SCOPES)
            credits=flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.json', 'w') as token:
            token.write(creds.to_json())

    service=build('gmail', 'v1', credentials=creds)

    sender='[email protected]'
    to='[email protected]'
    subject = 'Test Mail'
    message_text='This is a test email from the Gmail API.'

    message = create_message(sender, to, subject, message_text)
    send_message(service, 'me', message)

main()

oauth google-colaboratory gmail-api

2022-09-30 11:39

1 Answers

I have dealt with the Gmail API on Android as well and had similar problems.


To access the application being tested, test users can use the
·Authorization on the part of the test user
·Registration of test users on the project side
You will need two.

I think you are creating a project to use the Gmail API for Google Cloud Platform.
There is a part to register test users in API and Services → OAuth Consent Screen, so please try adding test users here.

Enter a description of the image here

(I'm sorry if the above answers are incorrect depending on the platform.)


2022-09-30 11:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.