适用于iOS的Google Sheet API的身份验证范围错误

时间:2019-04-28 15:42:10

标签: ios swift google-api google-sheets-api

我正在尝试使用适用于iOS的Google Sheets API来获取Google Sheet。我已经成功添加了用户身份验证,因为它不再要求API密钥,但是现在出现403错误:

Request had insufficient authentication scopes*.

我在Google Developers Console中添加了以下范围。

/auth/drive
/auth/spreadsheets

这是我用来执行和执行查询的代码。

let potentialShowSheetID = "1n4wvD2vSSiAnG_pnD9rWR6dNCSnZz0pAGAiSYRaJCKs"
let service = GTLRSheetsService()
private let scopes = [kGTLRAuthScopeSheetsSpreadsheets]  // This was specified in 
    // another Stack Overflow question, but I'm not sure how it would be used
    // and it doesn't seem to help.

override func viewDidLoad() {
    super.viewDidLoad()

    service.authorizer = GIDSignIn.sharedInstance().currentUser.authentication.fetcherAuthorizer()

    let query = GTLRSheetsQuery_SpreadsheetsGet.query(withSpreadsheetId: potentialShowSheetID)

    service.executeQuery(query) { (ticket, data, error) in
        print("GTLRService ticket: \(ticket)")
        if error != nil {
            print("GTLRService potential show query error: \(error!)")
        }
        else {
            print("data: \(data ?? "unknown data")")

        }
    }
}

是否可以使用iOS版Google Sheets API在查询中指定范围?我对框架进行了很好的浏览,似乎从来没有将其作为参数。也许我想念其他东西。

更新:我为用户检查了授予的范围,它不包括Drive或Sheets API。

print("Scopes:", GIDSignIn.sharedInstance()?.currentUser.grantedScopes ?? "unknown scopes")

    // Prints -> Scopes: [https://www.googleapis.com/auth/userinfo.profile, https://www.googleapis.com/auth/userinfo.email]

如何在身份验证期间指定授予访问权限的范围?

1 个答案:

答案 0 :(得分:0)

深入研究Google Sign In API标头后,我发现只需要使用以下方法指定范围:

GIDSignIn.sharedInstance()?.scopes = [kGTLRAuthScopeSheetsSpreadsheets, kGTLRAuthScopeCalendar]

...使用GIDSignInButton登录之前。