"Login Required" when uploading a file to Google Cloud Storage

319 views
Skip to first unread message

Michael Gaylord

unread,
Sep 25, 2014, 12:44:46 PM9/25/14
to google-api-obj...@googlegroups.com
I am having trouble figuring out how to upload a file to a Public Access bucket to Cloud Storage.

1. I have set up a bucket and set it's ACLs to READ and WRITE for all users.
2. I have enabled the Cloud Storage JSON API and the Cloud Storage API.
3. I have created an API key for browser applications 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

          storageObject.name = "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)")

                  return

              }

              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 (https://www.googleapis.com/upload/rpc?uploadType=resumable&prettyPrint=false) }

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

Error upload file: The operation couldnt be completed. (Login Required) : Optional("(Login Required)")


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


Thanks,

Michael

Greg Robbins

unread,
Sep 25, 2014, 8:22:28 PM9/25/14
to google-api-obj...@googlegroups.com
You can use the library's http logging feature to see the server requests and responses, https://code.google.com/p/google-api-objectivec-client/wiki/Introduction#Logging_HTTP_Server_Traffic

I'm not familiar with the Storage API, but I'd expect it to require an OAuth 2 authorizer rather than an API key when uploading.

For help using the Storage API, get an appropriate http log to show the relevant requests made by your client and the server responses, and use the support resources linked from https://cloud.google.com/storage/docs

Reply all
Reply to author
Forward
0 new messages