使用 iOS 将文件上传到 Google 驱动器时出现问题

Problems uploading files to Google Drive with iOS

我终于设法让 Google 驱动器 API 使用 iOS,使用 Swift。

我可以访问我的 Google 驱动器,并下载文件夹和文件名,所以我知道基本身份验证并且 API 正在工作。

问题是当我尝试上传一个小文件时出现了这个错误

reason: 'unexpected response data (uploading to the wrong URL?)
{"error":{"code":403,"message":"Insufficient Permission","data":[{"domain":"global","reason":"insufficientPermissions","message":"Insufficient Permission"}]},"id":"gtl_7"}'

我已经设置了一个看起来像有效的 OAuth 2.0 客户端 ID,但是当我第一次尝试这个时,我设置了错误的授权范围(来自我复制的示例)- 'kGTLAuthScopeDriveReadonly'。我现在已将其更改为 'kGTLAuthScopeDrive',但我担心该应用程序可能仍在使用原始范围,因为我无法强制重新登录。

这是我用来检查我是否已登录的代码

func checkAuthorisation()
{
if let auth = GTMOAuth2ViewControllerTouch.authForGoogleFromKeychainForName(
    kKeychainItemName,
    clientID: kClientID,
    clientSecret: kClientSecret)
    {
        service.authorizer = auth
    }
    else
    {
        presentViewController(
            createAuthController(),
            animated: true,
            completion: nil
        )
    }
}

这是我的上传代码

let metaData = GTLDriveFile()
metaData.title = "testfile.DAT"

let dataGoogleDrive = strMessage.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)
let uploadParamaters = GTLUploadParameters(data: dataGoogleDrive!, MIMEType: "text/plain")
let query = GTLQueryDrive.queryForFilesInsertWithObject(metaData, uploadParameters: uploadParamaters)

let serviceTicket = dataModel.googleDrive.service.executeQuery(query, completionHandler:
    {(ticket, file, error) -> Void in
        print("complete")
    })

我发现很多帖子描述了设置 Google 驱动器 API 的困难,但这不再是问题。

  1. 问题仅仅是因为我仍在使用错误范围的缓存凭据吗?如果是这样,我该如何冲洗它?
  2. 我需要在 Google 开发者控制台中配置什么吗?有一个选项可以将驱动器 API 配置为 'Allow users to create new documents using this application.',但是当我尝试使用它时,它不允许我保存更改。

您可以尝试进入 Google 帐户并撤销该应用程序的权限。然后它应该重新提示新范围