问题
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*.
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")")
}
}
}
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.
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?
回答1:
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]
...before signing in with GIDSignInButton
.
来源:https://stackoverflow.com/questions/55891880/insufficient-authentication-scopes-error-for-google-sheet-api-for-ios