我目前正在开发一个 iOS 应用程序,该应用程序需要写入登录用户拥有的谷歌表。
要登录我使用的用户 GoogleSignIn
pod 和附加到谷歌表我正在使用 GoogleAPIClientForREST/Sheets
荚。
我可以让用户正常登录,但在我的一生中,由于身份验证错误,我似乎无法写入工作表。我在 SO(如 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'
我的
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)
}
我尝试使用带有
authorization
的标准 URLSession标题如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()
但这也无法告诉我我需要传递访问 token 。谁能建议这应该如何工作?我似乎只是在循环使用文档和我能找到的所有其他示例似乎使用了似乎不存在的方法/属性!
这些是我正在使用的当前 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)
而且我只在知道我有用户时才调用写入工作表的方法。
谢谢
最佳答案
对于面临类似问题的任何其他人,我设法使用 GoogleAPIClient 解决了这个问题。
问题与 Swift 默默地删除前向声明的导入有关。
所以你需要一个桥接头如下
#import <GTMSessionFetcher/GTMSessionFetcher.h>
#import <GTMSessionFetcher/GTMSessionFetcherService.h>
并且这需要在您使用它的任何类中的 APIClient 框架之前导入。
例如
import GTMSessionFetcher
import GoogleAPIClientForREST
import GoogleSignIn
然后您应该可以访问
authorizer
属性(property)和fetcherAuthorizer()
方法!
关于ios - GoogleSignIn ios 附加到谷歌表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52762379/