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

Using oauth_from_dict not working #1524

Open
rayn316 opened this issue Oct 25, 2024 · 5 comments · May be fixed by #1525
Open

Using oauth_from_dict not working #1524

rayn316 opened this issue Oct 25, 2024 · 5 comments · May be fixed by #1525

Comments

@rayn316
Copy link

rayn316 commented Oct 25, 2024

Important: Please do not post usage questions here.
To get a quick response, please ask a question on Stack Overflow using gspread tag.
See existing questions: https://stackoverflow.com/questions/tagged/gspread


Describe the bug
A clear and concise description of what the bug is.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/dist-packages/gspread/auth.py", line 288, in oauth_from_dict
    if not creds and credentials is not None:
UnboundLocalError: local variable 'creds' referenced before assignment

To Reproduce
Steps to reproduce the behavior:

  1. run python
import gspread

credentials2 = {"installed":{"client_id":"222222","project_id":"222222","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"222222","redirect_uris":["http://localhost"]}}

gc, authorized_user = gspread.oauth_from_dict(credentials=credentials2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/dist-packages/gspread/auth.py", line 288, in oauth_from_dict
    if not creds and credentials is not None:
UnboundLocalError: local variable 'creds' referenced before assignment

Expected behavior
A clear and concise description of what you expected to happen.
The function executes normally.

Code example*
If applicable, provide a code example to help explain your problem.

Screenshots
If applicable, add screenshots to help explain your problem.
image

Environment info:

  • Operating System [e.g. Linux, Windows, macOS]: ubuntu 22.04
  • Python version: 3.10.12
  • gspread version: 6.1.4

Stack trace or other output that would be helpful

Additional context
Add any other context about the problem here.

@alifeee alifeee linked a pull request Oct 25, 2024 that will close this issue
@alifeee
Copy link
Collaborator

alifeee commented Oct 25, 2024

hi !

thanks for the report :) it does seem something is going wrong here

I have attempted a fix here: #1525

for now, you can try this code

credentials2 = ...

import json

CREDS_FILENAME = "credentials.json"
with open(CREDS_FILENAME, "w", encoding="utf-8") as file:
    json.dump(credentials2, file)

gc, authorized_user = gspread.oauth(credentials_filename=CREDS_FILENAME)

let me know if that helps :)

@rayn316
Copy link
Author

rayn316 commented Oct 28, 2024

Can't run, I need to run it on the server, but it will prompt
webbrowser.Error: could not locate runnable browser

@rayn316
Copy link
Author

rayn316 commented Oct 31, 2024

通过其它方式在服务器上执行成功了

需要先在本地执行获取authorized_user

import gspread
import json

credentials = {}
gc = gspread.oauth(credentials)

执行完成后,打开文件 C:\Users\username\AppData\Roaming\gspread\authorized_user.json
将内容作为authorized_user变量就可以在服务器上使用

import gspread
import json

credentials = {}
authorized_user = {}
gc, authorized_user = gspread.oauth_from_dict(credentials, authorized_user)
sh = gc.open_by_key('1PtyQXeetBjyJ7tSwR7XSXNXm1I.....')

@lavigne958
Copy link
Collaborator

通过其它方式在服务器上执行成功了

需要先在本地执行获取authorized_user

import gspread
import json

credentials = {}
gc = gspread.oauth(credentials)

执行完成后,打开文件 C:\Users\username\AppData\Roaming\gspread\authorized_user.json 将内容作为authorized_user变量就可以在服务器上使用

import gspread
import json

credentials = {}
authorized_user = {}
gc, authorized_user = gspread.oauth_from_dict(credentials, authorized_user)
sh = gc.open_by_key('1PtyQXeetBjyJ7tSwR7XSXNXm1I.....')

Hi please use English to reply to any issue, in case you need help you can use https://www.deepl.com/en/translator

@rayn316
Copy link
Author

rayn316 commented Oct 31, 2024

Executed successfully on the server through other methods

You need to execute locally first to get authorized_user

import gspread
import json

credentials = {}
gc = gspread.oauth(credentials)

After the execution is complete, open the file C:\Users\username\AppData\Roaming\gspread\authorized_user.json
and use the content as the authorized_user variable on the server

import gspread
import json

credentials = {}
authorized_user = {}
gc, authorized_user = gspread.oauth_from_dict(credentials, authorized_user)
sh = gc.open_by_key('1PtyQXeetBjyJ7tSwR7XSXNXm1I.....')

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

Successfully merging a pull request may close this issue.

3 participants