Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InstalledAppFlow automatically get authorization code

I am using InstalledAppFlow imported from google_auth_oauthlib.flow. Every time I run the code, I have to get the authorization code manually. The code will show like this:

Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=929791903032-hpdm8djidqd8o5nqg2gk636efau34ea6q.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%22F%2Fwww.googleapis.com%2Fauth%2Fyt-analytics-monetary.readonly&state=a0drph4tJFDc2rIGtdwUStIGbRKNnkv18&prompt=consent&access_type=offline

Enter the authorization code: Then I have to click to the accounts.google.com above to enter the authorization code then paste it: 4/0gCyNaDZeNRr5pnvkXFO1HvLjsq5IbjVYjETaUtlq8_1PqoaY9totzs

My question is how to do this step automatically not manually?

like image 668
Harry Avatar asked Feb 02 '26 20:02

Harry


1 Answers

Like tehhowch said their documentation which automates this process uses:

flow.run_local_server()

Rather than:

flow.run_console()

And then to automate it for next time you run it:

flow.run_local_server()
with open('token.pickle', 'wb') as token:
    pickle.dump(credentials, token)

It should work without prompts after the first run.

https://developers.google.com/apps-script/api/quickstart/python

like image 114
user7778287 Avatar answered Feb 05 '26 10:02

user7778287