IOS Upload File to Google Cloud Storage - Getting 401: "Login Required"

I am having trouble finding a file upload to my cloud storage shared storage.

  • I created a bucket and set its READ and WRITE ACLs for all users.
  • I have enabled JSON Cloud Storage API and Cloud Storage API.
  • I have created an API key for browser apps that allows any referrer.

Here is my code in Swift:

private lazy var googleServiceStorage:GTLServiceStorage = {
   var storage = GTLServiceStorage()
   storage.APIKey = "AIzaSy**********m8TPCM"
   storage.additionalHTTPHeaders = ["x-goog-project-id" : "159*******7"]
   return storage

public func uploadAssetToGoogle(resourcePath: String?) {
  if let _resourcePath = resourcePath {
      let fileHandle = NSFileHandle(forReadingAtPath: _resourcePath)
      let uploadParams = GTLUploadParameters(fileHandle: fileHandle, MIMEType: "video/mov")
      var storageObject = GTLStorageObject.object() as GTLStorageObject = "12345678"
      let query = GTLQueryStorage.queryForObjectsInsertWithObject(storageObject, bucket: "my-bucket", uploadParameters: uploadParams) as GTLQuery
      var ticket = googleServiceStorage.executeQuery(query) { ticket, object, error in
          if let _error = error {
              println("Error upload file: \(error.localizedDescription) : \(error.localizedFailureReason)")
          println("Upload succeeded")
      ticket.uploadProgressBlock = {ticket, numberOfBytesRead, dataLength in
          println("Ticket: \(ticket)")
          NSLog("read %llu from %llu bytes", numberOfBytesRead, dataLength)


When I call this code, I get the following output in the console:

mediaURL type: (Metatype) - file:///Users/Michael/Library/Developer/CoreSimulator/Devices/5895B7FA-41E7-4958-84FD-2C1043CA7CD7/data/Containers/Data/Application/2DD62539-E364-4BE0-A89C-E0DD2827D74B/tmp/trim.FCE68DAD-0FC6-4E2D-8C92-37055A02DD12.MOV

Ticket: GTLServiceTicket 0x7feed4860660: {service:<GTLServiceStorage: 0x7feed2cf9d90> devKey:AIzaSyBClcLHWtXzlBHb2VbATA1xIlUO0m8TPCM fetcher:GTMHTTPUploadFetcher 0x7feed4890fb0 ( } 

2014-09-25 18:25:23.515 Beta[14917:1971326] read 202 from 29512 bytes 

Error upload file: The operation couldn’t be completed. (Login Required) : Optional("(Login Required)")


There is something that I am missing, but I cannot figure it out. If anyone can help me it will save me a lot more hair.


source to share

1 answer

For the post Authorization Required , try adding a new bucket permission item

ENTITY = User, NAME = allUsers, ACCESS = Writer


Please note, you must use Server API key

notiOS API key

To load an object, you can add GTLStorageObjectAccessControl

GTLStorageObjectAccessControl *objAccessControl = [GTLStorageObjectAccessControl new];
objAccessControl.entity = @"allUsers"; = @"";
objAccessControl.role = @"OWNER";

GTLStorageObject *newObject = [GTLStorageObject object]; = @"image.png";
newObject.acl = @[objAccessControl];



GTLQueryStorage *query = [GTLQueryStorage queryForObjectsInsertWithObject:newObject bucket:kBucketName uploadParameters:uploadParameters];




All Articles