适用于iOS的Google Sheet API的身份验证范围错误 [英] Insufficient authentication scopes error for Google Sheet API for iOS
问题描述
我正在尝试使用适用于iOS的Google Sheets API来获取Google Sheet.我已经成功添加了用户身份验证,因为它不再要求API密钥,但现在出现403错误:
I'm trying to fetch a Google Sheet with the Google Sheets API for iOS. I've successfully added the user authentication since it's no longer asking for an API key, but I'm now getting a 403 error for:
Request had insufficient authentication scopes*.
我在Google Developers Console中添加了以下范围.
I've added the following scopes in the Google Developers Console.
/auth/drive
/auth/spreadsheets
这是我用来执行和执行查询的代码.
And here's the code I'm using to make and execute the query.
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在查询中指定范围?我对框架进行了很好的浏览,似乎从来没有将其作为参数.也许我想念其他东西.
Is there a way to specify the scope in the query with the Google Sheets API for iOS? I looked through the framework pretty well and it doesn't ever seem to take that as a parameter. Maybe I'm missing something else.
更新:我检查了用户的授予范围,它不包括Drive或Sheets API.
UPDATE: I checked the granted scopes for the user and it doesn't include the Drive or 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]
如何指定身份验证期间要授予访问权限的范围?
How can I specify the scopes to grant access to during authentication?
推荐答案
在对Google Sign In API标头进行了一些挖掘之后,我发现只需要为此指定范围:
After some digging around the Google Sign In API headers, I found I just had to specify the scopes with this:
GIDSignIn.sharedInstance()?.scopes = [kGTLRAuthScopeSheetsSpreadsheets, kGTLRAuthScopeCalendar]
...使用 GIDSignInButton
登录之前.
...before signing in with GIDSignInButton
.
这篇关于适用于iOS的Google Sheet API的身份验证范围错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!