0
votes

I'm trying to write to a Google Sheet from my swift project.

My podfile looks like:

platform :ios, '8.0'
target 'MyApp' do
     pod 'GoogleAPIClientForREST/Sheets', '~> 1.2.1'
     pod 'GoogleSignIn', '~> 4.1.1'
     pod 'Alamofire'
     pod 'GoogleAPIClientForREST'
 end

I have enabled the Google Drive and Google Sheets API on my developer console, and completed the integration steps necessary for Google sign in. I have the sign in part working properly and I am able to verify I have signed in.

I am trying to write to the sheet like this:

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) }

Which I referenced from this answer.

Now, the issue I am running into is that when I run that code to attempt to write to the sheet I am getting back this response (sheet ID has been intentionally removed by me):

    [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 }

In researching this more I found this as well which I thought would solve my issue but when I try to add GIDSignIn.sharedInstance()?.scopes = [kGTLRAuthScopeSheetsSpreadsheets] it tells me Cannot find 'kGTLRAuthScopeSheetsSpreadsheets' in scope.

I feel like at this point I am stuck for what I can try next. Any suggestions are appreciated!

1

1 Answers

0
votes

This answer is what I needed

I needed to add:

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/")