Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I send a message to a device using C2DM from a server that has been authenticated with OAuth2?
    text
    copied!<p>I'm developing the server part of a system that has to send messages to a device. This was working fine with the GoogleLogin method, but I want to migrate it to OAuth 2.0 since the other authentication method has been deprecated.</p> <p>In the Google API console I created a project and then I created a key for a service account.</p> <p>This is the code I'm using to authenticate the server:</p> <pre><code>public boolean authenticateServer(){ try { File privateKey = new File(getClass().getResource("/something-privatekey.p12").toURI()); GoogleCredential cred = new GoogleCredential.Builder().setTransport(new NetHttpTransport()) .setJsonFactory(new JacksonFactory()) .setServiceAccountId("something@developer.gserviceaccount.com") .setServiceAccountScopes("https://android.apis.google.com/c2dm") .setServiceAccountPrivateKeyFromP12File(privateKey) .addRefreshListener(this) .build(); boolean success = cred.refreshToken(); this.credential = cred; return success; } catch (Exception ex) { //handle this } return false; } </code></pre> <p>When I execute this, the method <code>onTokenResponse</code> gets called and I get an <code>access_token</code> with <code>token_type</code> "Bearer" expiring in 3600 seconds. So far, so good.</p> <p>This is the code I'm using to send the message to the device, which is always giving me a 401 status (not authorized). Any ideas?</p> <pre><code>private static String UTF8 = "UTF-8"; public void sendMessage(String text, String registrationId) { try { StringBuilder postDataBuilder = new StringBuilder(); postDataBuilder .append("registration_id") .append("=") .append(registrationId); postDataBuilder.append("&amp;") .append("collapse_key") .append("=") .append("0"); postDataBuilder.append("&amp;") .append("data.payload") .append("=") .append(URLEncoder.encode(text, UTF8)); byte[] postData = postDataBuilder.toString().getBytes(UTF8); URL url = new URL("https://android.apis.google.com/c2dm/send"); HostnameVerifier hVerifier = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setHostnameVerifier(hVerifier); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty( "Content-Length", Integer.toString(postData.length)); conn.setRequestProperty( "Authorization", "Bearer " + credential.getAccessToken()); OutputStream out = conn.getOutputStream(); out.write(postData); out.close(); int sw = conn.getResponseCode(); System.out.println("" + sw); } catch (IOException ex){ //handle this } } </code></pre> <p>Thanks!</p>
 

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