Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to open the sheet. #256

Closed
ghost opened this issue Jun 17, 2015 · 17 comments
Closed

Unable to open the sheet. #256

ghost opened this issue Jun 17, 2015 · 17 comments

Comments

@ghost
Copy link

ghost commented Jun 17, 2015

Code runs perfectly fine with no errors etc. But it is unable to open the sheet. Please refer the code below. Unable to figure whats wrong here. Thanks!

import os
import ast
import requests, gspread
from oauth2client.client import SignedJwtAssertionCredentials
def authenticate_google_docs():
    f = file(os.path.join('Shiva-c1c5516ecf00.p12'), 'rb')
    SIGNED_KEY = f.read()
    f.close()
    scope = ['https://spreadsheets.google.com/feeds', 'https://docs.google.com/feeds']
    credentials = SignedJwtAssertionCredentials('x@gmail.com', SIGNED_KEY, scope)
    data = {
        'refresh_token' : '241/ZLTD2DyOdiFJkN37mOZnmV-LtRu8wxY97o8Wn-nBkM4',
        'client_id' : '2434760852908-5vi7v8ebilde26mvlran6ike982ovuhs.apps.googleusercontent.com', 
        'client_secret' : '24EDI-HPmWBjvoTlm_NzJkcBmp',
        'grant_type' : 'refresh_token',
              }
    r = requests.post('https://accounts.google.com/o/oauth2/token', data = data)
    credentials.access_token = ast.literal_eval(r.text)['access_token']
    gc = gspread.authorize(credentials)
    return gc
gc = authenticate_google_docs()
sh = gc.open("Hero")

I tried sh=gc.open("Hero").sheet1 (didn't work either)

@burnash
Copy link
Owner

burnash commented Jun 17, 2015

What is your Python version? Did you share your spreadsheet with the email in json as the docs says?

Also, try using the code from the docs: http://gspread.readthedocs.org/en/latest/oauth2.html

Side note: please use code formatting in your markdown. It's not easy to read python without spaces.
Check GitHub's markdown reference.

@ghost
Copy link
Author

ghost commented Jun 17, 2015

Hi burnash,

I did share the client_email of my JSON file.

print(sys.version)
2.7.6 (default, Sep 9 2014, 15:04:36)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)]

I tried 2 ways using .p12 file & .JSON file. I have pasted the .p12 file code here in this issue. I have even tried .JSON one (http://gspread.readthedocs.org/en/latest/oauth2.html) you are pointing at. I am having same problem, code is working fine but I am unable to open the sheet. Is it mandatory to have 3.x python version, is it the real cause of the problem?. I am upgrading my version, anyways. Thanks!!

@burnash
Copy link
Owner

burnash commented Jun 17, 2015

No, you should be able to run it with 2.7.6 without any problem. Do you see any other spreadsheets?

Try to run gc.openall() after you logged in and check the returned value.

@ghost
Copy link
Author

ghost commented Jun 17, 2015

Running fine but not opening the sheet.

import json
import gspread
from oauth2client.client import SignedJwtAssertionCredentials
json_key = json.load(open('Shiva-05bfb3bd2059.json'))
scope = ['https://spreadsheets.google.com/feeds']
try:
    credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)
except TypeError:
    credentials = SignedJwtAssertionCredentials(json_key['client_email'], bytes(json_key['private_key'], 'utf-8'), scope)
    credentials = SignedJwtAssertionCredentials(json_key['client_email'], json_key['private_key'], scope)

gc = gspread.authorize(credentials)

sh = gc.openall()

@burnash
Copy link
Owner

burnash commented Jun 17, 2015

What is a value of sh?

@ghost
Copy link
Author

ghost commented Jun 17, 2015

Not sure, whats the value. I think, its an object which holds gc.open("some worksheet name"). Please correct me if I am wrong!!

@burnash
Copy link
Owner

burnash commented Jun 17, 2015

openall returns an array. I'm trying to find a source of the problem.

Try to do a print sh in your code above after you have sh = gc.openall() or do a print gc.openall() and post the output.

@ghost
Copy link
Author

ghost commented Jun 17, 2015

Thanks a ton. This the output. I have added the output as code because it Github is not accepting it.

[<gspread.models.Spreadsheet object at 0x10d970490>, <gspread.models.Spreadsheet object at 0x10d9704d0>]

@burnash
Copy link
Owner

burnash commented Jun 17, 2015

This looks like gspread has fetched 2 spreadsheets, and your authorization is working properly. You can go further and print titles of these spreadsheets:

for s in gc.openall():
    print s.title

The output will tell you what spreadsheets you have access to.

@ghost
Copy link
Author

ghost commented Jun 17, 2015

Awesome. Exactly, when it can print why its unable to open?

[<gspread.models.Spreadsheet object at 0x103185490>, <gspread.models.Spreadsheet object at 0x1031854d0>]

New Metrics
Hero

@burnash
Copy link
Owner

burnash commented Jun 18, 2015

This is strange. Try with your "New Metrics".

After you do sh = gc.open("Hero") (or "New Metrics"), run print sh.worksheets()

@ghost
Copy link
Author

ghost commented Jun 18, 2015

Yeah, I read so many docs regarding this, ran different scripts. I have no idea why is this happening. Is it more of http related one? unable to open a sheet because of http or missing any library or a file crucial to open the file which I missed to run?

This is the output for what you said. Once again, thanks for your time.

 [<gspread.models.Spreadsheet object at 0x101dbd490>, <gspread.models.Spreadsheet object at 0x101dbd4d0>]

New Metrics
Hero

[<Worksheet 'Sheet1' id:od6>]

I got same output for print sh.sheet1

I tried

sh.sheet1

this is the output

[<gspread.models.Spreadsheet object at 0x10a1f8490>, <gspread.models.Spreadsheet object at 0x10a1f84d0>]

New Metrics
Hero

I also tried sh = gc.open('Hero').sheet1. Below is the output

[<gspread.models.Spreadsheet object at 0x10b883490>, <gspread.models.Spreadsheet object at 0x10b8834d0>]

New Metrics
Hero
Traceback (most recent call last):
File "santa.py", line 20, in
print sh.sheet1
AttributeError: 'Worksheet' object has no attribute 'sheet1'

@burnash
Copy link
Owner

burnash commented Jun 18, 2015

[<Worksheet 'Sheet1' id:od6>]

This means that you do have a worksheet.

@ghost
Copy link
Author

ghost commented Jun 18, 2015

I am at the end of the road, but for some reason I am stuck. Its not working for some strange reason, Any alternatives to authorize, create & print something into Google Sheets other than GSpread?.

@burnash
Copy link
Owner

burnash commented Jun 18, 2015

I don't really understand why you're saying that you stuck. You have your worksheet object, just proceed with the docs and call the required methods on it. That's about it.

GData is an alternative Python client library from Google: https://github.com/google/gdata-python-client

@ghost
Copy link
Author

ghost commented Jun 19, 2015

Its working now. Thanks a ton @burnash. gc.open() didn't work at that point, now working fine. 👍

@arulmr
Copy link

arulmr commented Jun 20, 2015

@Shiva-Shinde If your issue is resolved, please close this issue.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants