Insufficient authentication scopes error for Google Sheet API for iOS

落爺英雄遲暮 提交于 2021-02-11 17:01:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!