使用iOS将文件上传到Google云端硬盘时出现问

时间:2015-12-02 17:40:49

标签: google-drive-api google-oauth

我终于使用Swift设法让Google Drive API与iOS配合使用。

我可以访问我的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 Drive API的困难,但这不再是问题。

  1. 只是因为我仍在使用带有错误范围的缓存凭据?如果是这样,我该如何冲洗呢?
  2. 我是否需要在Google Developer Console中配置一些内容?可以选择将Drive API配置为允许用户使用此应用程序创建新文档。'但是当我尝试使用它时,它不允许我保存更改。

1 个答案:

答案 0 :(得分:1)

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