I'm trying to upload image to Google Cloud Storage for a couple days. I tried to use Google APIs Client Library for Objective-C from this link https://code.google.com/p/google-api-objectivec-client/wiki/Introduction.
Here is my code implementation:
_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;
};
With that code, the error always appear "The operation couldn't be completed. (Access Not Configured. Please use Google Developers Console to activate the API for your project)". I don't know why error shows up whereas I've enabled all services related to storage like Google Cloud Storage and Google Cloud Storage JSON API and change ACL of the bucket so that all user on the internet can write/upload into it.
I also tried authentication process before upload. The code on 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];
});
When user press upload button:
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;
};
With that implementation I got different error, "The operation couldn't be completed. (Insufficient Permission)".
What should I do so that I can upload image successfully? Any help will be appreciated.
Thank you very much.