19

I wrote a Python script(Google_add.py) which creates users in the Google Workspace. However, When it is run, an AttributeError occurred. How do I resolve this Error?

・Google_add.py (Excerpt)

PATH = '/opt/Google/credentials.json'
CLIENT_SECRETS = '/opt/Google/client_secrets.json'
credentials = None

#If there are credentials
if os.path.exists(PATH):
    credentials = Credentials.from_authorized_user_file(
      path = PATH,
      scopes = SCOPES)

#If there are no (valid) credentials available
if not credentials or not credentials.valid:
    if credentials and credentials.expired and credentials.refresh_token:
      credentials.refresh(Request())
    else:
      FLOW = flow.InstalledAppFlow.from_client_secrets_file(
        client_secrets_file = CLIENT_SECRETS,
        scopes = SCOPES)
      credentials = FLOW.run_console()
    with open(PATH, 'w') as token:
      token.write(credentials.to_json())

・Error Message

File "Google_add.py", line 186, in <module>
main()
File "Google_add.py", line 172, in main
FLOW.run_console()
AttributeError: 'InstalledAppFlow' object has no attribute 'run_console'

I refer the below site when I wrote it. https://github.com/googleapis/google-api-python-client/blob/main/docs/oauth-installed.md#sending-users-to-googles-oauth-20-server https://developers.google.com/admin-sdk/directory/v1/quickstart/python?hl=en

In the script, I want to authorize using the user account(not service account). And "run_local_server()" method doesn't work because my linux server isn't a web server (doesn't have httpd, nginx and so on).

(I'm sorry if my writing is hard to read.)

3
  • In any case, have you seen this github post that has the same problem as yours? One of the comments suggested that the versions of installed packages be changed. Commented Mar 2, 2023 at 6:51
  • 1
    Thank you! The script runs successfully by changing the versions of google-api-python-client, google-auth, google-auth-httplib2, and google-auth-oauthlib. Commented Mar 2, 2023 at 11:10
  • I'm glad it worked, let me compose it as an answer. By doing so, other people in the community, who may have the same concern as you, will know that theirs can be resolved. Commented Mar 2, 2023 at 23:54

7 Answers 7

21

Expanding on George's answer above, the versions of the library from the github post that work are:

google-api-python-client==1.7.2
google-auth==1.8.0
google-auth-httplib2==0.0.3
google-auth-oauthlib==0.4.1
Sign up to request clarification or add additional context in comments.

Comments

12

You said:

"run_local_server()" method doesn't work because my linux server isn't a web server (doesn't have httpd, nginx and so on).

This isn't true. The solution below will work also on non-web servers, your local machine, local scripts etc... However, you need a way to open a browser. If not, you can create the token on your local machine and copy the token to your server.

Replace: credentials = FLOW.run_console()

with: credentials = FLOW.run_local_server(port=0)

This will work perfectly in all situations.

For more info, refer to https://developers.google.com/people/quickstart/python

Comments

7

The issue may have been a version compatibility issue of google-api-python-client library with other libraries like

  • google-auth
  • google-auth-httplib2
  • google-auth-oauthlib

You just need to make sure that the version of your google-api-python-client supports the versions you have installed to its dependency libraries. You can view the full change log of google-api-python-client library here

References:

google-api-python-client change log

https://googleapis.github.io/google-api-python-client/docs/auth.html

https://googleapis.github.io/google-api-python-client/docs/oauth.html

google-api-python-client

google_auth_oauthlib.flow module

Github post similar issue

1 Comment

This solution is outdated. It will result in a 400 error from Google OAuth.
5

The run_console() (OOB) flow has been deprecated by Google, that's why it was removed from newer versions of the Google OAuth connector

https://developers.googleblog.com/2022/02/making-oauth-flows-safer.html?m=1#disallowed-oob

Replace the line

credentials = FLOW.run_console()

to

credentials = FLOW.run_local_server()


people also got it working by these configurations:

google-api-python-client==1.7.2
google-auth==2.14.1
google-auth-httplib2==0.0.3
google-auth-oauthlib==0.4.1

git link : https://github.com/xyou365/AutoRclone/issues/101#issuecomment-1795530261

Comments

2

If you faced this problem using a VPS or because you rented gpu for your AI stuff, I suggest you do not downgrade the libraries and avoid using run_console. Instead, use run_local_server(port=8000, open_browser=False). The defined port could be anything, but make sure it's not already being used by other apps. After logging in through the given URL, you'll be redirected to localhost page, which has a similar link address to "http://localhost:8000/?state=...&code=...&scope=...". Since you are redirected to the localhost through your computer's browser, this won't reflect on the machine you are running your code. Instead, you have to open the terminal (on your server and not your local machine) and run curl "http://localhost:8000/?state=...&code=...&scope=..."

Comments

1

I just run the same code on another machine and copy over the saved token.json file onto the remote machine.

For example, in the google drive quickstart, I wanted to run it on a raspberry pi. But first I ran it on my local windows machine and it created the token.json file. I then used scp to copy it over.

Note though - you need to run this process each time the scopes change.

Comments

0

Replace: credentials = FLOW.run_console()

with: credentials = FLOW.run_local_server(port=0)

This worked. Thank you very much Ton Snoei.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.