Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 2
Registered: ‎06-28-2010
My Device: Tour 9630
My Carrier: Verizon
Accepted Solution

Getting 403:Forbidden from Google API - encoding problems in my POST?

Hi all,

I'm having trouble connecting to Google's API via POST using the instructions on this page:
http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#ClientLogin

I've also posted to the Google API forums but I'm starting to wonder if it's a problem that this forum would be able to answer. Here's the post on the Google forum:
http://www.google.com/support/forum/p/apps-apis/thread?fid=1c22cb44eb5cbba600048a1fc5f25621&hl=en


Google used to accept a GET request and provide an SID string that served as a login key for services like Docs and Calendar. It still provides this SID to GET requests, so I know I can make requests from the BB simulator, but it no longer accepts the SID as a login key. Now, I have to send a POST request in UTF-8 encoding, including parameters like my username and password, and get an AUTH token in response.

From the posts in this forum, I see that I can use a URLEncodedPostData object to encode the parameters for the POST in UTF-8. (I know that I'll eventually have to get signing keys to use this on a real BB, but I want to get a hello-world working on the simulator before I pony up for them.) I set the parameters in this object and include it in the request, but I always get a 403:Forbidden with an error message of "Error=BadAuthentication" in response, as though my password was wrong. I've checked the username/password carefully, and I can do the POST and get the AUTH token with curl from the command line on this machine, so it must be something about how I'm doing the POST in the Java code.

Someone on the Google forum suggested that my parameters may not have been encoded in UTF-8, but shouldn't the "UTF-8" parameter in the URLEncodedPostData constructor handle that encoding? I've also tried manually escaping the "@" in my email address to "%40" because it seems that URLEncodedPostData does not encode the @ symbol like this. (My password contains no characters that need escaping for URL encoding.)

Here's the code I'm using. I'm using the Eclipse IDE for Blackberry OS 5 (I think the version of the plugin for eclipse is 5.0.0_5.0.0.25) with the Verizon 9630 Tour simulator 5.0.0.591. Any help?

 

 

private void googleSignInTest(String emailAddress, String googlePassword){

// see http://code.google.com/apis/accounts/docs/AuthForInstalledApps.html#ClientLogin
String uri = "https://www.google.com/accounts/ClientLogin";

InputStream inputStream = null;
HttpsConnection httpsConnection = null;

try{
//create https connection, POST
httpsConnection = (HttpsConnection)Connector.open(uri);
httpsConnection.setRequestMethod(HttpsConnection.POST);

//http://supportforums.blackberry.com/t5/Java-Development/HttpConnection-set-to-POST-does-not-work/m-p...
URLEncodedPostData postData = new URLEncodedPostData("UTF-8", false);
postData.append("accountType", "GOOGLE");
postData.append("Email", emailAddress);
postData.append("Passwd", googlePassword);
postData.append("service", "cl");
postData.append("source", "my-test-app");
postData.append("Content-Type", "application/x-www-form-urlencoded");
postData.append("Content-Language", "en-US");

DataOutputStream outputStream = httpsConnection.openDataOutputStream();
outputStream.write(postData.toString().getBytes());
outputStream.flush();
outputStream.close();

//make the request and get response
int responseCode = httpsConnection.getResponseCode(); 
//403 Forbidden Error=BadAuthentication

//Do something with the response
...
}catch (IOException e) {
e.printStackTrace();
}

 

 

New Contributor
Posts: 2
Registered: ‎06-28-2010
My Device: Tour 9630
My Carrier: Verizon

Re: Getting 403:Forbidden from Google API - encoding problems in my POST?

Seems that I have to set the content type in the connection and not in the post data:

 

httpsConnection.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, HttpProtocolConstants.CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED);

 Also, the following line is probably not right:

 

 

 

outputStream.write(postData.toString().getBytes());

 

 

It should probably be:

 

 

outputStream.write(postData.getBytes());

 

 

Highlighted
New Contributor
Posts: 7
Registered: ‎07-18-2011
My Device: none
My Carrier: none

Re: Getting 403:Forbidden from Google API - encoding problems in my POST?

Setting the content type as above is also necessary if you what to submit to a Google Spreadsheet Form using POST.

Thanks,