- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这几天我一直在尝试将图片上传到 Google Cloud Storage。我尝试通过此链接将 Google API 客户端库用于 Objective-C https://code.google.com/p/google-api-objectivec-client/wiki/Introduction .
这是我的代码实现:
_serviceStorage = [GTLServiceStorage new];
_serviceStorage.APIKey = API_KEY;
_serviceStorage.additionalHTTPHeaders = @{@"x-goog-project-id": PROJECT_ID};
GTLUploadParameters *uploadParam = [GTLUploadParameters uploadParametersWithData:_imageData MIMEType:@"image/jpeg"];
GTLStorageObject *storageObj = [GTLStorageObject object];
GTLQueryStorage *query = [GTLQueryStorage queryForObjectsInsertWithObject:storageObj bucket:BUCKET_NAME uploadParameters:uploadParam];
GTLServiceTicket *ticket = [_serviceStorage executeQuery:query delegate:self didFinishSelector:@selector(serviceTicket:finishedWithObject:error:)];
ticket.uploadProgressBlock = ^(GTLServiceTicket *ticket,
unsigned long long numberOfBytesRead,
unsigned long long dataLength) {
self.progressView.progress = (float)numberOfBytesRead/(float)dataLength;
};
使用该代码,错误总是出现“操作无法完成。(访问未配置。请使用 Google Developers Console 为您的项目激活 API)”。我不知道为什么会出现错误,而我已经启用了所有与存储相关的服务,例如 Google Cloud Storage 和 Google Cloud Storage JSON API,并更改了存储桶的 ACL,以便互联网上的所有用户都可以写入/上传到其中。
我还尝试了上传前的身份验证过程。viewDidLoad 上的代码:
_serviceStorage = [GTLServiceStorage new];
_serviceStorage.additionalHTTPHeaders = @{@"x-goog-project-id": PROJECT_ID};
GTMOAuth2ViewControllerTouch *oAuthVC = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kScope
clientID:kClientID
clientSecret:kClientSecret
keychainItemName:kKeychainItemName
completionHandler:^(GTMOAuth2ViewControllerTouch *viewController, GTMOAuth2Authentication *auth, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});
auth.authorizationTokenKey = @"access_token";
_serviceStorage.authorizer = auth;
}];
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:oAuthVC animated:YES completion:nil];
});
当用户按下上传按钮时:
GTLUploadParameters *uploadParam = [GTLUploadParameters uploadParametersWithData:_imageData MIMEType:@"image/jpeg"];
GTLStorageObject *storageObj = [GTLStorageObject object];
GTLQueryStorage *query = [GTLQueryStorage queryForObjectsInsertWithObject:storageObj bucket:BUCKET_NAME uploadParameters:uploadParam];
GTLServiceTicket *ticket = [_serviceStorage executeQuery:query delegate:self didFinishSelector:@selector(serviceTicket:finishedWithObject:error:)];
ticket.uploadProgressBlock = ^(GTLServiceTicket *ticket,
unsigned long long numberOfBytesRead,
unsigned long long dataLength) {
self.progressView.progress = (float)numberOfBytesRead/(float)dataLength;
};
通过该实现,我得到了不同的错误,“操作无法完成。(权限不足)”。
我应该怎么做才能成功上传图片?任何帮助将不胜感激。
非常感谢。
最佳答案
我终于找到了答案。这是验证代码:
GTMOAuth2ViewControllerTouch *oAuthVC = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:kGTLAuthScopeStorageDevstorageReadWrite
clientID:kClientID
clientSecret:kClientSecret
keychainItemName:kKeychainItemName
completionHandler:^(GTMOAuth2ViewControllerTouch *viewController, GTMOAuth2Authentication *auth, NSError *error) {
_accessToken = [NSString stringWithFormat:@"Bearer %@", [auth.parameters objectForKey:@"access_token"]];
_serviceStorage.additionalHTTPHeaders = @{@"x-goog-project-id": PROJECT_ID, @"Content-Type": @"application/json-rpc", @"Accept": @"application/json-rpc", @"Authorization": _accessToken};
_serviceStorage.authorizer = auth;
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});
}];
这里是上传过程:
GTLUploadParameters *uploadParam = [GTLUploadParameters uploadParametersWithData:_imageData MIMEType:@"image/jpeg"];
GTLStorageObject *storageObj = [GTLStorageObject object];
storageObj.name = FILE_NAME;
GTLQueryStorage *query = [GTLQueryStorage queryForObjectsInsertWithObject:storageObj bucket:BUCKET_NAME uploadParameters:uploadParam];
GTLServiceTicket *ticket = [_serviceStorage executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
}];
ticket.uploadProgressBlock = ^(GTLServiceTicket *ticket,
unsigned long long numberOfBytesRead,
unsigned long long dataLength) {
self.progressView.progress = (float)numberOfBytesRead/(float)dataLength;
};
或者,如果您想在不进行身份验证的情况下上传,则必须使用服务器应用程序的 key 而不是iOS 应用程序的 key 来分配 API key
_serviceStorage.APIKey = KEY_SERVER_APPLICATION;
我不知道为什么在使用 服务器应用程序 key 时上传过程成功,而这是一个 iOS 应用程序
关于ios - 上传谷歌云存储 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22343889/
我有以下正则表达式 /[a-zA-Z0-9_-]/ 当字符串只包含从 a 到z 大小写、数字、_ 和 -。 我的代码有什么问题? 能否请您向我提供一个简短的解释和有关如何修复它的代码示例? //var
我是一名优秀的程序员,十分优秀!