Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect to Google Cloud Messaging (GCM) server using Google App Engine (GAE)

I am trying to set up google cloud messaging for my app, and I am using Google App Engine for my server. I have my API key but I can't seem to make a connection to the google cloud messaging servers. Here is my code.

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://android.googleapis.com/gcm/send");
        try {

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("registration_id", regId));
            nameValuePairs.add(new BasicNameValuePair("data.message", messageText));    

            post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            post.setHeader("Authorization", "key=*MY_API_KEY_HERE*");
            post.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");


            Header[] s=post.getAllHeaders();


            System.out.println("The header from the httpclient:");

            for(int i=0; i < s.length; i++){
            Header hd = s[i];

            System.out.println("Header Name: "+hd.getName()
                    +"       "+" Header Value: "+ hd.getValue());
            }


            HttpResponse response = client.execute(post);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = "";
            while ((line = rd.readLine()) != null) {
            System.out.println(line);
            }

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

When I look at the log the headers are being setup right. However, I get an error that says

org.apache.http.impl.client.DefaultRequestDirector tryConnect: I/O exception (java.net.SocketException) caught when connecting to the target host: Permission denied: Attempt to access a blocked recipient without permission. (mapped-IPv4)

I've turned google cloud messaging on in the Google APIs Console and I've checked my API key a bunch of times. I have no clue why I'm getting rejected. Is there a jar I need in the war or something I have to put in the manifest?

Thanks for reading this!! Mark

like image 619
Mark Wang Avatar asked Sep 08 '13 06:09

Mark Wang


1 Answers

I had the same problem and I was using something similar to what you are using.

  1. I had to enable billing on my GAE app ( which you probably have but I wasn't aware that I would have to)
  2. Read https://developers.google.com/appengine/docs/java/sockets/ and https://developers.google.com/appengine/docs/java/urlfetch/

Therefore my code which looked like yours before now looks like following:

String json ="{}"; 
URL url = new URL("https://android.googleapis.com/gcm/send");
HTTPRequest request = new HTTPRequest(url, HTTPMethod.POST);
request.addHeader(new HTTPHeader("Content-Type","application/json")); 
request.addHeader(new HTTPHeader("Authorization", "key=<>"));
request.setPayload(json.getBytes("UTF-8"));
HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(request);
like image 148
Nikunj Yadav Avatar answered Sep 24 '22 15:09

Nikunj Yadav