I am trying to set up a Google Drive API instance with Python to download files. I set up an OAuth 2.0 Client ID on my Google APIs Console for a specific project and have enabled the Google Drive API client. However, I keep getting a redirect_uri_mismatch error when testing on localhost. I read somewhere to include http://localhost:8080 as the redirect URI for testing, however, the redirect_uri_mismatch error keeps throwing the error on a random port on localhost. I am not sure how to set up a correct port for localhost so this error doesn't keep getting thrown. It currently says that the redirect URI the instance is trying to use is http://localhost:49334/. If I add that URI to my credentials.json file, then it just throws another mismatch error over a different port. How do I fix this issue? I am exactly using code from this documentation: https://developers.google.com/drive/api/v3/quickstart/python and https://developers.google.com/drive/api/v3/manage-downloads.
Asked
Active
Viewed 2,543 times
1
-
In order to correctly understand about your current situation, can you provide your current script for replicating your issue? By the way, from your question, I thought that you might create the credential files including the client secret as the web application. In that case, you have set `http://localhost:8080` to the redirect uri? – Tanaike Jul 19 '20 at 22:02
-
The current script is on this documentation I posted: https://developers.google.com/drive/api/v3/quickstart/python. I don't do anything different to the code, exact same code. Also yes in the credentials.json file that you download from Google APIs Console's credentials section, I set the redirect uri to `http://localhost:8080`. – nosh Jul 19 '20 at 22:25
-
Thank you for replying. From your replying, I proposed the modification points as an answer. Could you please confirm it? If that was not the solution of your issue, I apologize. – Tanaike Jul 19 '20 at 23:13
1 Answers
5
Please modify as follows.
Please set the redirect uri at "Google APIs Console's credentials section" of Google Cloud Platform as follows. When you modified it, please save it. And please confirm whether the redirect url was modified.
From
http://localhost:8080
To
http://localhost:8080/
Please modify your script as follows. From your replying, I understand that you are using the script of Quickstart for python.
From
creds = flow.run_local_server(port=0)
To
creds = flow.run_local_server(port=8080)
Run the script and authorize.

Tanaike
- 181,128
- 11
- 97
- 165
-
Okay, it worked. So after signing in once, do I never have to sign in again because it generates a `token.pickle` file? I want this implemented on my backend which is being hosted by Heroku. – nosh Jul 19 '20 at 23:50
-
@nenur Thank you for replying. I'm glad your issue was resolved. When `token.pickle` is created, the refresh token is included in the file. So after 2nd run, you can retrieve the access token using the refresh token. So you are not required to use the browser. But please check the refresh token expiration at [the official document](https://developers.google.com/identity/protocols/oauth2#expiration). – Tanaike Jul 20 '20 at 00:14
-
is the `flow.run_local_server(port=8080)` only used if running the server over `localhost`? What about if I try to redirect to https://example.com? How should the code look then, or are there no changes needed to be implemented? – nosh Jul 22 '20 at 20:01