0

I'm trying to move a Python Jupyter scraper script (and json cred file) from my laptop to Google Colab. I've made a connection between Google Colab and Google Drive. I've stored the (.ipynb) script and credential JSON file on Google Drive. However I can't make the connection between the 2 (gdrive json cred file and colab) to make it work. Here below the part of the script concerning the credentials handling:

# Sheet key
# 1i1bmMt-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_d7Eo

import gspread
import pandas as pd
import requests
from bs4 import BeautifulSoup
from oauth2client.service_account import ServiceAccountCredentials

# Access credentials for google sheet and access the google sheet
scope = ["https://spreadsheets.google.com/feeds",
         "https://www.googleapis.com/auth/spreadsheets",
         "https://www.googleapis.com/auth/drive.file",
         "https://www.googleapis.com/auth/drive"]

# Copy your path to your credential JSON file.
PATH_TO_CREDENTIAL = '/Users/user/json-keys/client_secret.json'

# Initiate your credential
credentials = ServiceAccountCredentials.from_json_keyfile_name(PATH_TO_CREDENTIAL, scope)

# Authorize your connection to your google sheet
gc = gspread.authorize(credentials)

I receive FileNotFoundError: and credential erros Hope someone can help me with this, thanks

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Tom
  • 127
  • 1
  • 13

1 Answers1

-1

You try to put the file to the same directory to test it first. Make sure that the file is okay and can run successfully.

Here's the source code for reference:

If client_secret.json is in the same directory as the file you're running, then the correct syntax is:

import os
DIRNAME = os.path.dirname(__file__)
credentials = ServiceAccountCredentials.from_json_keyfile_name(os.path.join(DIRNAME, 'client_secret.json'), scope)

If the above test is okay, then try to move the file to your target directory '/Users/user/json-keys/client_secret.json' and try to create a symbolic link in the current directory to link the client_secret.json file. Then, run the program with the above code to test it again. Make sure it has no problem when putting the file to that directory. It's a workaround.

I used this case for reference to this: Django not recognizing or seeing JSON file

Monique G.
  • 239
  • 1
  • 6
  • thnx. The .ipynb and .json are both in the same "Colab Notebooks" file on gdrive. When I use your syntax I receive a "NameError: name '__file__' is not defined" on "---> 18 DIRNAME = os.path.dirname(__file__)". Do I need to specify the folder or file? – Tom Jan 20 '21 at 07:47
  • You can check on this case: https://stackoverflow.com/questions/16771894/python-nameerror-global-name-file-is-not-defined there are several ways to fix that error. @Tom – Monique G. Jan 20 '21 at 20:55
  • thnx , but unfortunately couldn't solve it with any of the given solutions in the threads. After that I tried mounting the gdrive to colab with: "from google.colab import drive drive.mount('/content/gdrive')" and after that I used the old json file path: PATH_TO_CREDENTIAL = '/content/gdrive/MyDrive/Colab Notebooks/client_secret.json' and now it works :) – Tom Jan 21 '21 at 09:50