GoogleSignIn iOS附加到Google Sheet [英] GoogleSignIn ios append to google sheets

查看:37
本文介绍了GoogleSignIn iOS附加到Google Sheet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前正在开发一个iOS应用程序,该应用程序需要写入登录用户拥有的Google工作表。

要登录用户,我使用的是GoogleSignInPod,要追加到Google工作表,我使用的是GoogleAPIClientForREST/SheetsPod。

我可以让用户登录,但在我的生命中,由于身份验证错误,我似乎不能写入工作表。我在so(LikeUpdating specific row in iOS Swift using Google SpreadSheet API)等上找到的每个示例都声明使用当前用户的fetcherAuthorizer()方法并将其分配给service.authorizer,但是这甚至不会编译,并显示以下错误value of type 'GIDAuthentication?' has no member 'fetcherAuthorizer'

对于我的播客文件,我有:

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()方法!

这篇关于GoogleSignIn iOS附加到Google Sheet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆