Search code examples
javaandroidpicasa

Picasa API Upload Photo: Getting 403/Invalid Token


I am trying to upload photos from an Android application to Picasa. I've signed in using Google Single Sign On and retrieved the access token from there.

When trying to create a new album, I am getting a similar error that returns Invalid Token.

I can make GET calls that retrieve public albums and photos in those albums, but no POST requests.

Here is how I am trying to upload a photo from storage

String url = "https://picasaweb.google.com/data/feed/api/user/" + mPreferences.getUsername() + "/albumid/" + mAlbumID;
HttpClient httpClient = new DefaultHttpClient();

File file = new File("/storage/emulated/0/Pictures/Screenshots/scrn.png");

HttpPost httpPost = new HttpPost(url);
httpPost.setHeader("GData-Version", "2");
httpPost.setHeader("Content-type", "image/jpeg");
httpPost.setHeader("Slug", "plz-to-love-realcat.jpg");
httpPost.setHeader("Authorization", "GoogleLogin auth=" + mPreferences.getAccessToken());

InputStreamEntity reqEntity;
org.apache.http.HttpResponse response;

try {
    reqEntity = new InputStreamEntity(new FileInputStream(file), file.length());

    String CONTENTTYPE_BINARY = "binary/octet-stream";
    reqEntity.setContentType(CONTENTTYPE_BINARY);
    reqEntity.setChunked(true);
    httpPost.setEntity(reqEntity);
    response = httpClient.execute(httpPost);

    Log.d("Picasa Upload", "STATUS CODE : " + response.getStatusLine().getStatusCode());

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (ClientProtocolException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Solution

  • Ended up solving this by changing

    httpPost.setHeader("Authorization", "GoogleLogin auth=" + mPreferences.getAccessToken());
    

    to

    httpPost.setHeader("Authorization", "OAuth " + mPreferences.getAccessToken());