当前位置: 首页 > 知识库问答 >
问题:

请求的身份验证范围不足Google Sheets API Swift

都超英
2023-03-14

我正试着从我的swift项目中写到谷歌表单。

platform :ios, '8.0'
target 'MyApp' do
     pod 'GoogleAPIClientForREST/Sheets', '~> 1.2.1'
     pod 'GoogleSignIn', '~> 4.1.1'
     pod 'Alamofire'
     pod 'GoogleAPIClientForREST'
 end
let sheetID = "*************"
    let range = "A3:B4"
    let requestParams = [
        "values": [
            ["hi1", "hi2"],
            ["hi3", "hi4"]
            ]
        ]
    let accessToken = GIDSignIn.sharedInstance().currentUser.authentication.accessToken!
    let header = ["Authorization":"Bearer \(accessToken)"]
    let requestURL = "https://sheets.googleapis.com/v4/spreadsheets/\(sheetID)/values/\(range)?valueInputOption=USER_ENTERED"
    let req = Alamofire.request(requestURL, method: .put, parameters: requestParams, encoding: JSONEncoding.default, headers: header)
    req.responseJSON { response in debugPrint(response) }

现在,我遇到的问题是,当我运行该代码试图写入工作表时,我得到的响应是这样的(工作表ID被我故意删除了):

    [Request]: PUT https://sheets.googleapis.com/v4/spreadsheets/**********/values/A3:B4?valueInputOption=USER_ENTERED
[Request Body]: 
{"values":[["hi1","hi2"],["hi3","hi4"]]}
[Response]: <NSHTTPURLResponse: 0x2801e1160> { URL: https://sheets.googleapis.com/v4/spreadsheets/*******/values/A3:B4?valueInputOption=USER_ENTERED } { Status Code: 403, Headers {
    "Alt-Svc" =     (
        "h3-Q050=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000,h3-T051=\":443\"; ma=2592000,h3-T050=\":443\"; ma=2592000,h3-Q046=\":443\"; ma=2592000,h3-Q043=\":443\"; ma=2592000,quic=\":443\"; ma=2592000; v=\"46,43\""
    );
    "Cache-Control" =     (
        private
    );
    "Content-Encoding" =     (
        gzip
    );
    "Content-Length" =     (
        137
    );
    "Content-Type" =     (
        "application/json; charset=UTF-8"
    );
    Date =     (
        "Tue, 10 Nov 2020 14:16:00 GMT"
    );
    Server =     (
        ESF
    );
    Vary =     (
        Origin,
        "X-Origin",
        Referer
    );
    "Www-Authenticate" =     (
        "Bearer realm=\"https://accounts.google.com/\", error=\"insufficient_scope\", scope=\"https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file https://www.googleapis.com/auth/drive.resource https://www.googleapis.com/auth/spreadsheets https://spreadsheets.google.com/feeds https://spreadsheets.google.com/feeds/ http://spreadsheets.google.com/feeds http://spreadsheets.google.com/feeds/ https://spreadsheets.google.com/feeds/spreadsheets https://spreadsheets.google.com/feeds/spreadsheets/private/full http://spreadsheets.google.com/feeds/spreadsheets/private/full https://spreadsheets.google.com/feeds/worksheets/ https://spreadsheets.google.com/tq https://spreadsheets.google.com/feeds/list/ https://spreadsheets.google.com/feeds/worksheet/ https://spreadsheets.google.com/feeds/cell/\""
    );
    "x-frame-options" =     (
        SAMEORIGIN
    );
    "x-xss-protection" =     (
        0
    );
} }
[Response Body]: 
{
  "error": {
    "code": 403,
    "message": "Request had insufficient authentication scopes.",
    "status": "PERMISSION_DENIED"
  }
}

[Result]: SUCCESS
[Timeline]: Timeline: { "Request Start Time": 626710559.941, "Initial Response Time": 626710560.249, "Request Completed Time": 626710560.250, "Serialization Completed Time": 626710560.252, "Latency": 0.308 secs, "Request Duration": 0.310 secs, "Serialization Duration": 0.001 secs, "Total Duration": 0.311 secs }

在进一步研究中,我还发现了这一点,我认为这将解决我的问题,但当我尝试添加GidSignin.SharedInstance()?.scopes=[kGTLRAuthScopeSheetsSpreadsheets]时,它告诉我在scope中找不到'kGTLRAuthScopeSheetsSpreadsheets'。

我觉得在这一点上,我被困在我下一步可以尝试的东西上了。如有任何建议,不胜感激!

共有1个答案

洪捷
2023-03-14

这个答案是我需要的

我需要补充一句:

GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/drive")
            GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/drive.file")
            GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/drive.resource")
            GIDSignIn.sharedInstance().scopes.append("https://www.googleapis.com/auth/spreadsheets")
            GIDSignIn.sharedInstance().scopes.append("https://spreadsheets.google.com/feeds")
            GIDSignIn.sharedInstance().scopes.append("https://spreadsheets.google.com/feeds/spreadsheets")
            GIDSignIn.sharedInstance().scopes.append("https://spreadsheets.google.com/feeds/spreadsheets/private/full")
            GIDSignIn.sharedInstance().scopes.append("https://spreadsheets.google.com/feeds/worksheets/")
            GIDSignIn.sharedInstance().scopes.append("https://spreadsheets.google.com/tq")
            GIDSignIn.sharedInstance().scopes.append("https://spreadsheets.google.com/feeds/list/")
            GIDSignIn.sharedInstance().scopes.append("https://spreadsheets.google.com/feeds")
            GIDSignIn.sharedInstance().scopes.append("https://spreadsheets.google.com/feeds/cell/")
 类似资料:
  • 我第一次尝试使用Google Firestore,通过Google api身份验证进行身份验证。我要做的第一件事是用几个方法来填充数据库,当我等待批处理任务时,会出现以下错误: 我几乎找不到谷歌认证连接firestore的方法。首先,我使用google console为我的应用程序提供的密码使用此方法进行身份验证: 使用在我的google控制台中为应用程序配置的相同作用域(我在同一应用程序中使用g

  • null 所用程序: 获取身份验证响应后创建Google analytics service对象。/li> 脚本: null

  • 这是我为Google Sheets API编写的代码。我有这是我找到的解决此问题的方法,但这并不能解决我的问题。我已经删除了我的.Credentials文件夹,它不提示我登录。

  • 我是新来的,我不是程序员,那我需要你的帮助。 我正在测试说明管理老师和学生在这里找到:https://developers.google.com/classroom/guides/manage-users 我尝试使用身份验证和作用域,就像在PHP QuickStart中一样:https://developers.google.com/classroom/quickstart/php 我成功运行了q

  • 我已成功完成此处的快速入门演示: https://developers.google.com/drive/api/v3/quickstart/python 我想更进一步,开始上传文件和文件夹。为此,我将范围替换为:SCOPES=['https://www.googleapis.com/auth/drive'] 我也已经把行刑换成了 然后我得到一个错误:GoogleAppClient。错误。Http