我目前正在开发一个iOS应用,该应用需要写入登录用户所拥有的Google工作表。
要登录用户,我要使用GoogleSignIn
Pod;要附加到Google工作表,请使用GoogleAPIClientForREST/Sheets
Pod。
我可以让用户正确登录,但是由于身份验证错误,我一生似乎无法写工作表。我在SO上找到的每个示例(例如,使用Google SpreadSheet API更新iOS Swift中的特定行)之类的状态都使用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()
但这也无法告诉我我需要传递访问令牌。谁能建议这应该如何工作?我似乎只是在与文档打交道,我能找到的所有其他示例似乎都在使用似乎不存在的方法/属性!
这些是我正在使用的当前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
属性和fetcherAuthorizer()
方法!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句