1

这是我使用谷歌客户端 api 将图像上传到谷歌云存储的代码

_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] 错误域 = com.google.GTLJSONRPCErrorDomain 代码 = 401“(需要登录)” UserInfo = {错误 = 需要登录,GTLStructuredError = GTLErrorObject 0x7b88d7e0:{消息:“需要登录”代码:401 数据:[1]},NSLocalizedFailureReason=(需要登录)}

我该如何解决?

4

0 回答 0