Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

difficulty getting oauth file with yagmail #261

Open
kramsman opened this issue Dec 6, 2022 · 4 comments
Open

difficulty getting oauth file with yagmail #261

kramsman opened this issue Dec 6, 2022 · 4 comments

Comments

@kramsman
Copy link

kramsman commented Dec 6, 2022

Dear Github,

I am looking forward to using yagmail, but am having a devil of a time getting authorization to work. After first trying to set my gmail account to less secure, I learned this apparently is no longer allowed and Oauth 2 is required.

My issue is much like the one specified in this thread, . As stated in this post, #143 (comment), it appears that google does not supply the credential file in the correct format. But I tried a bunch of things and each had its own problem.

  • When I set up a Client ID in the Google API console, download the json as credentials.json and let the system create the token.json, things work to a point: I am brought through google to "pick an account, do you want to continue" and token is created. I am able to print labels for the gmail account. But when I issue yag.send(to='xxx@gmail.com', subject='Testing Yagmail', contents='Hurray, it worked!'), I get an error "TypeError: refresh_authorization() got an unexpected keyword argument 'token'." When I look at the token file, it does contain the key 'token' which it should not per Oauth2 issue: TypeError: refresh_authorization() got an unexpected keyword argument 'installed' #143 (comment).
  • If I go into the token and edit it to reflect the the expected contents as identified in the above link by removing keys that are not specified and prefixing the names with 'google_', I get an error "ValueError: Authorized user info was not in the expected format, missing fields refresh_token, client_id, client_secret." It doesn't seem to like the 'google_' prefix.
  • editing the token file as above without the 'google_' prefix seems to get further producing a different error "An error occurred: <HttpError 403 when requesting https://gmail.googleapis.com/gmail/v1/users/me/labels?alt=json returned "Request had insufficient authentication scopes"

I am stuck. Relatively new to Oauth2, but it seems others are able to use yagmail. Is there a trick I am missing? I am also new to Github but hope my post gives the right amount of info.

Thanks for any assistance,
Brian

@kpeters58
Copy link

Here's what ChatGPT suggested when I posted your question:

It sounds like you are having trouble getting yagmail to work with OAuth2 authentication. This can be a tricky process, but I will do my best to help you troubleshoot the issue.

First, make sure that you have followed the steps outlined in the yagmail documentation for setting up OAuth2 authentication. This includes creating a project in the Google Cloud Console, enabling the Gmail API, creating OAuth2 credentials, and configuring yagmail to use the credentials.

If you have followed these steps and are still having trouble, there are a few things you can try:

Double-check your OAuth2 credentials: Make sure that you have downloaded the JSON file containing your OAuth2 credentials from the Google Cloud Console, and that you have specified the correct path to this file in yagmail. Also, make sure that you have specified the correct email address for the user parameter in yagmail, which should be the same email address associated with the OAuth2 credentials.

Check your OAuth2 token: If you are able to authenticate with Google and generate a token, but are still getting errors when trying to use yagmail, it's possible that your OAuth2 token is not being passed correctly to yagmail. You can try manually specifying the token file in yagmail using the oauth2_file parameter. For example:

python
Copy code
yag = yagmail.SMTP('your_email@gmail.com', oauth2_file='/path/to/token.json')
Make sure that the token file contains the correct fields, including access_token, expires_in, and token_type, and that the values are correctly formatted.

Check your authorization scopes: The error message you are seeing about "insufficient authentication scopes" suggests that you may not have requested the correct authorization scopes when setting up your OAuth2 credentials. Make sure that you have requested the Gmail API scope https://www.googleapis.com/auth/gmail.send, and that you have authorized your credentials with this scope. You can check this by going to the Google Cloud Console, selecting your project, and clicking on "APIs & Services" > "Credentials". Under "OAuth 2.0 Client IDs", click on your client ID and make sure that the "Authorized redirect URIs" includes urn:ietf:wg:oauth:2.0:oob (if you are using the "installed application" flow) or your actual redirect URI (if you are using the "web application" flow). Also make sure that the "Scopes" section includes the https://www.googleapis.com/auth/gmail.send scope.

I hope that these suggestions are helpful in getting your yagmail authentication working with OAuth2.

@sumba101
Copy link

Dear Github,

I am looking forward to using yagmail, but am having a devil of a time getting authorization to work. After first trying to set my gmail account to less secure, I learned this apparently is no longer allowed and Oauth 2 is required.

My issue is much like the one specified in this thread, . As stated in this post, #143 (comment), it appears that google does not supply the credential file in the correct format. But I tried a bunch of things and each had its own problem.

  • When I set up a Client ID in the Google API console, download the json as credentials.json and let the system create the token.json, things work to a point: I am brought through google to "pick an account, do you want to continue" and token is created. I am able to print labels for the gmail account. But when I issue yag.send(to='xxx@gmail.com', subject='Testing Yagmail', contents='Hurray, it worked!'), I get an error "TypeError: refresh_authorization() got an unexpected keyword argument 'token'." When I look at the token file, it does contain the key 'token' which it should not per Oauth2 issue: TypeError: refresh_authorization() got an unexpected keyword argument 'installed' #143 (comment).
  • If I go into the token and edit it to reflect the the expected contents as identified in the above link by removing keys that are not specified and prefixing the names with 'google_', I get an error "ValueError: Authorized user info was not in the expected format, missing fields refresh_token, client_id, client_secret." It doesn't seem to like the 'google_' prefix.
  • editing the token file as above without the 'google_' prefix seems to get further producing a different error "An error occurred: <HttpError 403 when requesting https://gmail.googleapis.com/gmail/v1/users/me/labels?alt=json returned "Request had insufficient authentication scopes"

I am stuck. Relatively new to Oauth2, but it seems others are able to use yagmail. Is there a trick I am missing? I am also new to Github but hope my post gives the right amount of info.

Thanks for any assistance, Brian

were you able to solve this issue? i am facing the same problem now.

@kramsman
Copy link
Author

I got it working, and it was really useful, but I need to research the fix. I'm now running my app on Google Cloud Functions and am using Sendgrid, very similar to Yagmail, because there was more documentation for using it with Functions.

@kramsman
Copy link
Author

kramsman commented May 19, 2023

I have a working version. What step did you get to and what problem did you have? The key is navigating to the url posted in the python console which brings up the Google authorization steps BUT ENDS IN WHAT APPEARS TO BE AN ERROR, "This site can’t be reached - localhost refused to connect". You then need to copy the link from this page , the one in your browser window that caused the error, and paste it into your python console at the prompt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants