Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting 403 (Forbidden) when attempting to POST file to Google Cloud Storage from service account
    primarykey
    data
    text
    <p>I'm wondering if anyone can tell me the proper syntax &amp; formatting for a service account to send a POST Object to bucket request? I'm attempting it programmatically using <a href="http://hc.apache.org/user-docs.html" rel="nofollow">the HttpComponents library</a>. I manage to get a token from my GoogleCredential, but every time I construct the POST request, I get:</p> <blockquote> <p>HTTP/1.1 403 Forbidden</p> <p><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt;&lt;Error&gt;&lt;Code&gt;AccessDenied&lt;/Code&gt;&lt;Message&gt;Access denied.&lt;/Message&gt;&lt;Details</code>>bucket-name<code>&lt;/Details&gt;&lt;/Error</code>></p> </blockquote> <p>The Google <a href="http://developers.google.com/storage/docs/reference-methods" rel="nofollow">documentation</a> that describes the request methods, mentions posting using html forms, but I'm hoping that wasn't suggesting the ONLY way to get the job done. I know that HttpComponents has a way to explicitly create form data by using <a href="http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/entity/UrlEncodedFormEntity.html" rel="nofollow">UrlEncodedFormEntity</a>, but it doesn't support multipart data. Which is why I went with using the MultipartEntity class. My code is below:</p> <pre><code>MultipartEntity entity = new MultipartEntity( HttpMultipartMode.BROWSER_COMPATIBLE ); String token = credential.getAccessToken(); entity.addPart("Authorization", new StringBody("OAuth " + token)); String date = formatDate(new Date()); entity.addPart("Date", new StringBody(date)); entity.addPart("Content-Type", new StringBody("multipart/form-data")); entity.addPart("bucket", new StringBody(bucket)); entity.addPart("key", new StringBody("fileName")); entity.addPart("success_action_redirect", new StringBody("/storage")); File uploadFile = new File("pathToFile"); FileBody fileBody = new FileBody(uploadFile, "text/xml"); entity.addPart("file", fileBody); httppost.setEntity(entity); System.out.println("Posting URI = "+httppost.toString()); HttpResponse response = client.execute(httppost); HttpEntity resp_entity = response.getEntity(); </code></pre> <p>As I mentioned, I am able to get an actual token, so I'm pretty sure the problem is in how I've formed the request as opposed to not being properly authenticated.</p> <p>Keep in mind:</p> <ol> <li>This is being performed by a service account.</li> <li>Which means that it does have Read/Write access</li> </ol> <p>Thanks for reading, and I appreciate any help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload