GoogleSignIn ios 附加到谷歌表格

Tom*_*yBs 4 ios google-signin google-sheets-api

我目前正在开发一个 iOS 应用程序,该应用程序需要写入登录用户拥有的谷歌表。

要登录我正在使用GoogleSignInpod的用户,并附加到我正在使用GoogleAPIClientForREST/Sheetspod的 google 工作表。

我可以让用户正常登录,但在我的一生中,由于身份验证错误,我似乎无法写入工作表。我在 SO 上找到的每个示例(例如使用 Google SpreadSheet API 更新 iOS Swift 中的特定行等都声明使用fetcherAuthorizer()当前用户的方法并将其分配给service.authorizer,但是这甚至不会编译并出现以下错误value of type 'GIDAuthentication?' has no member 'fetcherAuthorizer'

所以对于我的 podfile 我有:

pod 'GoogleSignIn'
pod 'GoogleAPIClientForREST/Sheets' 
Run Code Online (Sandbox Code Playgroud)

我的AppDelegate didFinishLaunchingWithOptions有以下几点:

    GIDSignIn.sharedInstance().clientID = "clientIdHere"

    GIDSignIn.sharedInstance()?.scopes.append(kGTLRAuthScopeSheetsSpreadsheets)
    GIDSignIn.sharedInstance()?.scopes.append(kGTLRAuthScopeSheetsDrive)
    GIDSignIn.sharedInstance()?.delegate = self
Run Code Online (Sandbox Code Playgroud)

当我尝试写入工作表时,我使用:

    let service = GTLRSheetsService()
     // following line errors with authorizer not being a property of the service 
     //variable and fetchAuthorizer() not being a method on authentication
service.authorizer = GIDSignIn.sharedInstance().currentUser.authentication.fetcherAuthorizer()
        let spreadsheetId = "id"
        let range = "Sheet1"
        let valueRange = GTLRSheets_ValueRange.init();
        valueRange.values = [
            ["Hello", "World"]
        ]
        let query = GTLRSheetsQuery_SpreadsheetsValuesAppend
            .query(withObject: valueRange, spreadsheetId:spreadsheetId, range:range)
        query.valueInputOption = "USER_ENTERED"

        service.executeQuery(query) { (ticket, any, error) in
            if let error = error {
                print(error)
            }
            print(any)
            print(ticket)
        }
Run Code Online (Sandbox Code Playgroud)

我试过使用标准的 URLSession 和一个authorization标头,例如

let data = ["range":"Sheet1!A1:D1", "majorDimension":"ROWS", "values": ["123","123","123","123"]] as [String : Any]
    urlRequest.addValue("application/json", forHTTPHeaderField: "content-type")
    urlRequest.addValue(GIDSignIn.sharedInstance()!.currentUser.authentication.accessToken, forHTTPHeaderField: "authorization")
    urlRequest.httpBody = try! JSONSerialization.data(withJSONObject: data, options: .sortedKeys)
    let session = URLSession.shared.dataTask(with: urlRequest) { (data, response, error) in
        if let error = error {
            print(error)
        } else {
            print(String(data: data!, encoding: .utf8))
        }
    }.resume()
Run Code Online (Sandbox Code Playgroud)

但这也无法告诉我我需要传递访问令牌。谁能建议这应该如何工作?我似乎只是在循环使用文档和我能找到的所有其他示例似乎使用了似乎不存在的方法/属性!

这些是我正在使用的当前 pod 版本

Using GTMOAuth2 (1.1.6)
Using GTMSessionFetcher (1.2.0)
Using GoogleAPIClientForREST (1.3.6)
Using GoogleSignIn (4.2.0)
Using GoogleToolboxForMac (2.1.4)
Run Code Online (Sandbox Code Playgroud)

而且我只在知道我有用户时才调用写入工作表的方法。

谢谢

Tom*_*yBs 9

对于面临类似问题的任何其他人,我设法使用 GoogleAPIClient 解决了这个问题。

问题与 Swift默默地删除前向声明的导入有关。

所以你需要一个桥接头如下

#import <GTMSessionFetcher/GTMSessionFetcher.h>
#import <GTMSessionFetcher/GTMSessionFetcherService.h>
Run Code Online (Sandbox Code Playgroud)

并且这需要在您使用它的任何类中的 APIClient 框架之前导入。

例如

import GTMSessionFetcher
import GoogleAPIClientForREST
import GoogleSignIn
Run Code Online (Sandbox Code Playgroud)

然后您应该能够访问该authorizer属性和fetcherAuthorizer()方法!

  • 你的回答拯救了我的@$$ @TommyBs。对于任何感兴趣的人:Swift 5(在 Xcode 11.2 中运行)不再需要桥接标头,只需将“import GTMSessionFetcher”添加到您的文件中就足够了 (3认同)