これは、GoogleクライアントAPIを使用してGoogleクラウドストレージに画像をアップロードするための私のコードです
_serviceStorage= [GTLServiceStorage new];
_serviceStorage.APIKey = GoogleAPI ;
NSString *const kKeychainItemName = @"StorageSample: Google Cloud Storage";
GTMOAuth2ViewControllerTouch *oAuthVC = [[GTMOAuth2ViewControllerTouch alloc]
initWithScope:kGTLAuthScopeStorageDevstorageReadWrite
clientID:GoogleClientID
clientSecret:GoogleClientSecret
keychainItemName:kKeychainItemName
completionHandler:^(GTMOAuth2ViewControllerTouch *viewController, GTMOAuth2Authentication *auth, NSError *error) {
NSString * _accessToken = [NSString stringWithFormat:@"Bearer %@", [auth.parameters objectForKey:@"access_token"]];
_serviceStorage.additionalHTTPHeaders = @{@"x-goog-project-id": GoogleProjectID, @"Content-Type": @"application/json-rpc", @"Accept": @"application/json-rpc", @"Authorization": _accessToken};
_serviceStorage.authorizer = auth;
_serviceStorage.retryEnabled = YES;
dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});
}];
UIImage * img = [UIImage imageNamed:@"icon-user-default.png"];
NSData * imageData = UIImagePNGRepresentation(img);
GTLUploadParameters *uploadParam = [GTLUploadParameters uploadParametersWithData:imageData MIMEType:@"image/jpeg"];
GTLStorageObject *storageObj = [GTLStorageObject object];
storageObj.name = @"cc.png";
GTLQueryStorage *query = [GTLQueryStorage queryForObjectsInsertWithObject:storageObj bucket:@"my-test-app" uploadParameters:uploadParam];
GTLServiceTicket *ticket = [_serviceStorage executeQuery:query completionHandler:^(GTLServiceTicket *ticket, id object, NSError *error) {
if (error) {
NSLog(@"%@",error.debugDescription);
}
}];
ticket.uploadProgressBlock = ^(GTLServiceTicket *ticket,
unsigned long long numberOfBytesRead,
unsigned long long dataLength) {
//self.progressView.progress = (float)numberOfBytesRead/(float)dataLength;
};
それは常にエラーを表示します
2015-10-14 12:41:44.399 Pix[21848:145791] エラー Domain=com.google.GTLJSONRPCErrorDomain Code=401 "(Login Required)" UserInfo={error=Login Required, GTLStructuredError=GTLErrorObject 0x7b88d7e0: {message:"ログインが必要" コード:401 データ:[1]}、NSLocalizedFailureReason=(ログインが必要)}
どうすれば修正できますか?