【问题标题】:gspread and oauth2client usagegspread 和 oauth2client 的使用
【发布时间】:2016-07-12 17:27:46
【问题描述】:

我试图使用 oauth2client 和 gspread 在 googlesheet 上进行操作,但我遇到的问题是,当使用 oauth2client 时,它需要一个范围字段。我不知道范围是什么。以下是oauth2client的使用代码。

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds']

credentials = ServiceAccountCredentials.from_json_keyfile_name('gspread-april-2cd … ba4.json', scope)

gc = gspread.authorize(credentials)

wks = gc.open("Where is the money Lebowski?").sheet1

【问题讨论】:

  • 您无法打开电子表格吗?或者您只是想知道范围是什么?
  • @Burnash 我弄清楚了范围,但我随后使用了 open_with_url 并直接复制了我的电子表格的 url,当我尝试我的代码时它显示“没有找到电子表格”。我创建的电子表格来自我创建 Google 项目的同一个帐户(或 API,我忘了它叫什么)
  • 您是否在 gspread-april-2cd...ba4.json 中与 client_email 共享了电子表格
  • 那个 gspread-xxxx.json 文件是我创建项目时下载的文件,对吧? client_email 应该是我创建项目的 Google 帐户?
  • 是的,gspread-xxxx.json 是您在创建凭据时下载的文件。 client_email此文件中的参数,而不是 google 帐户。

标签: oauth google-oauth google-spreadsheet-api gspread oauth2client


【解决方案1】:

OAuth 范围表示您请求用户为您的应用授权的权限。

使用Google Sheets API v3时,您使用的范围是授权您的请求的唯一可用范围。

https://spreadsheets.google.com/feeds

如果您使用了Google Sheets API v4,您现在可以使用以下两个作用域中的任何一个:

https://www.googleapis.com/auth/spreadsheets.readonly

允许对用户的工作表及其属性进行只读访问,并且

https://www.googleapis.com/auth/spreadsheets

允许对用户的工作表及其属性进行读/写访问。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多