我目前正在开发一个 iOS 应用程序,该应用程序需要写入登录用户拥有的 Google 工作表。
要登录我正在使用的用户GoogleSignIn
pod 并附加到我正在使用的谷歌表GoogleAPIClientForREST/Sheets
pod.
我可以让用户正常登录,但由于身份验证错误,我似乎无法在工作表中写入内容。我在 SO 上找到的每个例子(比如使用 Google SpreadSheet API 更新 iOS Swift 中的特定行 https://stackoverflow.com/questions/51585042/updating-specific-row-in-ios-swift-using-google-spreadsheet-api)等类似的状态使用fetcherAuthorizer()
当前用户的方法并将其分配给service.authorizer
,但是这甚至不会编译并出现以下错误value of type 'GIDAuthentication?' has no member 'fetcherAuthorizer'
所以对于我的 podfile 我有:
pod 'GoogleSignIn'
pod 'GoogleAPIClientForREST/Sheets'
My AppDelegate didFinishLaunchingWithOptions
有以下内容:
GIDSignIn.sharedInstance().clientID = "clientIdHere"
GIDSignIn.sharedInstance()?.scopes.append(kGTLRAuthScopeSheetsSpreadsheets)
GIDSignIn.sharedInstance()?.scopes.append(kGTLRAuthScopeSheetsDrive)
GIDSignIn.sharedInstance()?.delegate = self
当我尝试写入工作表时,我使用:
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)
}
我尝试过使用标准 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()
但这也无法告诉我我需要传递访问令牌。谁能建议这应该如何运作?我似乎只是在与文档打交道,我能找到的所有其他示例似乎都使用了似乎不存在的方法/属性!
这些是我当前使用的 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)
当我知道我有用户时,我只会调用写入工作表的方法。
Thanks