difficulty getting oauth 2 token file with yagmail

323 views Asked by At

I am trying to use the python package yagmail to send emails but am having a tough time getting authorization to work.

My issue is getting an Oauth 2 token, but there is a disconnect with yagmail, as specified in a github thread. As stated in this post, https://github.com/kootenpv/yagmail/issues/143, it appears that google does not supply the credential file in the correct format. But I tried a bunch of things and each has 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='[email protected]', 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 this github post https://github.com/kootenpv/yagmail/issues/143#issuecomment-527115298][2].
  • 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 originally posted on Github because I found that other related post, but it seems SO is more geared toward Q&A. Is there a relation betweeen Github and SO? Difference?

Thanks for any assistance, Brian

1

There are 1 answers

0
kramsman On

I finally found a solution and the answer was hidden in plain sight.

First the Oauth authorization needed to be set up as outlined in this post (which is excellent): Sending email via Gmail & Python

As stated, when yagmail is run the first time the authorization process gives instructions, the final stating to "Navigate to the following URL to auth:" and asks "Enter the localhost URL you were redirected to:"

The problem is the browser window shows what appears to be an error message, a sad face with a message "This site can’t be reached, localhost refused to connect, reload", etc. I never thought this was expected behavior. The url is the one being navigated to in the error screen.

Simply stating the error should be expected, and the url needs to be copied and pasted in the post above would help a lot.