Firebase 身份验证 - Googlesheets "The request is missing a valid API key." 错误 403

Firebase Authentication - Googlesheets "The request is missing a valid API key." Error 403

尝试将 Firebase 项目连接到 GoogleSheets 时出现错误

(lldb) po error
Error Domain=com.google.GTLRErrorObjectDomain Code=403 "The request is missing a valid API key." UserInfo={GTLRStructuredError=GTLRErrorObject 0x61000005c650: {errors:[1] message:"The request is missing a valid API key." code:403 status:"PERMISSION_DENIED"}, NSLocalizedDescription=The request is missing a valid API key.}

这是我的代码

let query = GTLRSheetsQuery_SpreadsheetsValuesGet
  .query(withSpreadsheetId: spreadsheetId, range:range)
service.executeQuery(query,
                     delegate: self,
                     didFinish: #selector(displayResultWithTicket(ticket:finishedWithObject:error:))

func displayResultWithTicket(ticket: GTLRServiceTicket,
                           finishedWithObject result : GTLRSheets_ValueRange,
                           error : NSError?) {

if let error = error {
  showAlert(title: "Error", message: error.localizedDescription)
  return
}

我看到一个类似的问题

我在 https://console.developers.google.com/ 启用了 Google 表格 API 并刷新 GoogleService-Info.plist。从模拟器中删除应用程序,重置模拟器并将其添加回去。

但我仍然得到错误

我正在使用

我正在使用范围 kGTLRAuthScopeSheetsSpreadsheetsReadonly

我在 api 资源管理器中执行没有问题

GET https://sheets.googleapis.com/v4/spreadsheets/1o0kCH8As6Zx5q-n8KykaQS053ZkXMzLlc6PWiVnHipw?ranges=A1%3AB2&key={YOUR_API_KEY}

我尝试重新生成 api 密钥并重新下载 google.plist

我解决了这个问题。

  1. 这与 Firebase 无关,是 GoogleAPIClientForREST 问题

  2. 我安装了 GoogleAPIClientForREST (1.3.0) 而不是 1.2.1

  3. 我没有使用来自 GIDGoogleUserauthentication fetcherAuthorizer 的 GTMFetcherAuthorizationProtocol 传递 service.authorizer ()

在 1.3.0 中,服务 no 不再有授权者我找不到关于正确执行此方法的文档,所以我恢复到 1.2.1

Google 文档和快速入门已过时

-------------------------------------------- ---------- 更新 -------------------------- --------------------------

GoogleSheets 工程师的回复在线程 groups.google.com/forum

如果您没有桥接头,请添加桥接接头

  #import <GTMSessionFetcher/GTMSessionFetcher.h>       
  #import <GTMSessionFetcher/GTMSessionFetcherService.h>

这将解决 1.3.0 的问题