Sending message to c2dm with oauth 2 and getting 401

208 views
Skip to first unread message

park alert

unread,
May 23, 2012, 11:20:01 AM5/23/12
to androi...@googlegroups.com

Hi everyone.

I have a version of my app without oauth authentication and it works fine. But i want to migrate to oauth 2. I'm have a problem when tying to send a message, i keep getting 401 unauthorized 
I already sign up for c2dm (twice), refreshed multiple times the authentication token and I'm using the same email in both server and device. Does anyone have some idea why this is happening?

public static String getToken() {

JsonFactory jsonFactory = new JacksonFactory();
HttpTransport httpTransport = new NetHttpTransport();        

String clientId = "****.apps.googleusercontent.com";
String pkcs12Repo = "*****privatekey.p12";

GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId(clientId)
.setServiceAccountPrivateKeyFromP12File(new File(pkcs12Repo))
.setServiceAccountScopes(scope)
.build();

credential.refreshToken();
String token = credential.getAccessToken();
return token;
}

public static int sendMessage(String auth_token, String registrationId, String message) {

StringBuilder postDataBuilder = new StringBuilder();
postDataBuilder.append("registration_id").append("=").append(registrationId);
postDataBuilder.append("&").append("collapse_key").append("=").append("0");
postDataBuilder.append("&").append("data.payload").append("=").append(URLEncoder.encode(message, "UTF-8"));

byte[] postData = postDataBuilder.toString().getBytes("UTF-8");

HttpsURLConnection.setDefaultHostnameVerifier(new CustomizedHostnameVerifier());
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
conn.setRequestProperty("Content-Length", Integer.toString(postData.length));
conn.setRequestProperty("Authorization", "GoogleLogin auth=" + auth_token);

OutputStream out = conn.getOutputStream();
out.write(postData);
out.close();

int responseCode = conn.getResponseCode();
return responseCode;
}


Felipe Leme

unread,
May 23, 2012, 12:17:18 PM5/23/12
to androi...@googlegroups.com

park alert

unread,
May 24, 2012, 3:06:28 AM5/24/12
to androi...@googlegroups.com

The scope for requesting the authentication token works with both options, https://android.apis.google.com/c2dm and https://android.apis.google.com/c2dm/send.
But I'm still getting the unauthorized response when sending a message.

Devika

unread,
May 25, 2012, 4:59:05 AM5/25/12
to android-c2dm
I am also facing this same issue. Please post if anybody finds
solution.

On May 24, 12:06 pm, park alert <park.alert.codeb...@gmail.com> wrote:
> The scope for requesting the authentication token works with both options, *https://android.apis.google.com/c2dmand **https://android.apis.google.com/c2dm/send.*
> *But I'm still getting the unauthorized response when sending a message.*
> *
> *
>
>
>
>
>
>
>
> On Wednesday, May 23, 2012 5:17:18 PM UTC+1, Felipe Leme wrote:
>
> > The scope should be *https://android.apis.google.com/c2dm/send*, not just
> > *https://android.apis.google.com/c2dm*.
>
> > On Wed, May 23, 2012 at 8:20 AM, park alert <park.alert.codeb...@gmail.com

Devika

unread,
May 25, 2012, 8:19:31 AM5/25/12
to android-c2dm
Got it working at last!!!
you need to send authorization token in the header part as
post.addHeader("Authorization","Bearer "+ access_token);

On May 25, 1:59 pm, Devika <orioninc.a...@gmail.com> wrote:
> I am also facing this same issue. Please post if anybody finds
> solution.
>
> On May 24, 12:06 pm, park alert <park.alert.codeb...@gmail.com> wrote:
>
>
>
>
>
>
>
> > The scope for requesting the authentication token works with both options, *https://android.apis.google.com/c2dmand**https://android.apis.google.com/c2dm/send.*

park alert

unread,
May 25, 2012, 11:43:18 AM5/25/12
to android-c2dm

Could you show how you are sending the message? I already tried that
and no results :/


On May 25, 1:19 pm, Devika <orioninc.a...@gmail.com> wrote:
> Got it working at last!!!
> you need to send authorization token in the header part as
> post.addHeader("Authorization","Bearer "+ access_token);
>
> On May 25, 1:59 pm, Devika <orioninc.a...@gmail.com> wrote:
>
>
>
>
>
>
>
> > I am also facing this same issue. Please post if anybody finds
> > solution.
>
> > On May 24, 12:06 pm, park alert <park.alert.codeb...@gmail.com> wrote:
>
> > > The scope for requesting the authentication token works with both options, *https://android.apis.google.com/c2dmand**https://android.apis.google....

park alert

unread,
May 27, 2012, 2:36:15 PM5/27/12
to androi...@googlegroups.com

I already search everywhere to get an answer but nothing until now. I really don't know what am I doing wrong.

chandrapal singh

unread,
May 28, 2012, 4:45:44 AM5/28/12
to androi...@googlegroups.com
I think this can be proxy issue.

pls try to connect your PC with other internet device/connection. some time due to company polocy it ask for password for connecting internet.

Hope this will help.



--
Thanks and Regards
Chandrapal Singh

Devika

unread,
May 28, 2012, 5:26:29 AM5/28/12
to android-c2dm
this is my code.

public void sendMessage1(PrintWriter out,String access_token,String
message,String key) {
DefaultHttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(authUrl);
post.addHeader("Authorization","Bearer "+ access_token);
ArrayList<BasicNameValuePair> paramList = new
ArrayList<BasicNameValuePair>();
paramList.add(new BasicNameValuePair("registration_id", regId));
paramList.add(new BasicNameValuePair("collapse_key", key));
paramList.add(new BasicNameValuePair("data."+"message", message));

try {
post.setEntity(new UrlEncodedFormEntity(paramList, HTTP.UTF_8));
HttpResponse response = client.execute(post);
out.print(EntityUtils.toString(response.getEntity()));
} catch (UnsupportedEncodingException e) {
throw new C2DMException(e);
} catch (ClientProtocolException e) {
throw new C2DMException(e);
} catch (IOException e) {
throw new C2DMException(e);
}

}

Hope this helps.

park alert

unread,
May 30, 2012, 12:37:57 PM5/30/12
to androi...@googlegroups.com

I am doing exactly the same thing. The problem seems to be:

Authentication error: Unable to respond to any of these challenges: {}

<HTML>
<HEAD>
<TITLE>Unauthorized</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Unauthorized</H1>
<H2>Error 401</H2>
</BODY>
</HTML>
Reply all
Reply to author
Forward
0 new messages