无法使用Google云端硬盘API将图片上传到Google云端硬盘
问题描述:
我正尝试使用Swift中的Google云端硬盘API将图像上传到Google云端硬盘。但得到以下错误信息: - “ ”意外的响应数据(上传到错误的URL?) {“error”:{“code”:401,“message”:“Login Required”,“data”:[{“域“:”全球“,”原因“:”必需“,”消息“:”需要登录“,”位置类型“:”标题“,”位置“:”授权“}]},”id“:”gtl_3“ }无法使用Google云端硬盘API将图片上传到Google云端硬盘
她是我的代码 -
func uploadFileToDrive() {
print("uploading Photo")
let dateFormat = NSDateFormatter()
dateFormat.dateFormat = "'Quickstart Uploaded File ('EEEE MMMM d, YYYY h:mm a, zzz')"
let file = GTLDriveFile()
file.name = dateFormat.stringFromDate(NSDate())
file.descriptionProperty = "Uploaded from Google Drive IOS"
file.mimeType = "image/png"
let fileManager = NSFileManager.defaultManager()
let urls = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
// If array of path is empty the document folder not found
guard urls.count == 0 else {
let imageURL = urls.first!.URLByAppendingPathComponent("image.png")
// Data object to fetch image data
do {
let imageData = try NSData(contentsOfURL: imageURL, options: NSDataReadingOptions())
let uploadParameters = GTLUploadParameters(data: imageData, MIMEType: file.mimeType)
let query = GTLQueryDrive.queryForFilesCreateWithObject(file, uploadParameters: uploadParameters) as GTLQueryDrive
self.driveService.executeQuery(query, completionHandler: { (ticket, insertedFile , error) -> Void in
let myFile = insertedFile as? GTLDriveFile
if error == nil {
print("File ID \(myFile?.identifier)")
} else {
print("An Error Occurred! \(error)")
}
})
} catch {
print(error)
}
return
}
}
请帮我什么,我做错了什么?
答
您需要身份验证才能上传到Google云端硬盘。使用Drive API with iOS的身份验证过程如下:
- 在GDC中为您的应用程序创建一个oAuthclientID。
-
设置范围。 NSString * keychainItemName = @“My App”;
NSString示波器=
@"https://www.googleapis.com/auth/drive.file"
NSString * keychainItemName = @“MyApp”;
NSString * clientId = @“PUT_CLIENT_ID_HERE”; 检查存储的凭证。
- 如有必要请求授权。
- 处理认证响应。
认证和授权完成后,您现在可以Upload in Google Drive。
+0
谢谢。我会尝试。 –
错误很明显:您需要进行身份验证。 –
如何验证上传URL? @AlexandreCartapanis –
我不知道ios,尝试谷歌搜索“ios swift2 google-drive-sdk authenticate” –