Skip to content
Advertisement

localhost refused to connect in a databricks notebook calling the google api

I read the Google API documentation pages (Drive API, pyDrive) and created a databricks notebook to connect to the Google drive. I used the sample code in the documentation page as follow:

JavaScript

The CRED_PATH includes the credential file path in /dbfs/FileStore/shared_uploads. The script prompts me the URL to authorize the application but immediately after allowing access it redirects to the page that says “This site can’t be reached: localhost refused to connect.”
The localhost is listening on the default port (8080):
enter image description here
I checked the redirect URI of the registered app in Google API Services and it includes the localhost.
I’m not sure what should I check/set to have access the Google API in databricks. Any thought is appreciated

Advertisement

Answer

Although I’m not sure whether this is better workaround for your situation, in your situation, how about using the service account instead of OAuth2 you are using? By this, the access token can be retrieved without opening the URL for retrieving the authorization code, and Drive API can be used with googleapis for python you are using. From this, I thought that your issue might be able to be removed.

The method for using the service account with your script is as follows.

Usage:

1. Create service account.

About this, you can see the following official document.

and/or

When the service account is created, the credential file of JSON data is downloaded. This file is used for the script.

2. Sample script:

The sample script for using the service account with googleapis for python is as follows.

JavaScript

Note:

  • The Google Drive of the service account is different from your Google Drive. So in this case, when you share a folder on your Google Drive with the mail address of the service account (This email address can be seen in the credential file.). By this, you can get and put the file to the folder using the service account and you can see and edit the file in the folder on your Google Drive using the browser.

References:

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement