Sushil Kandola
Sushil Kandola

Reputation: 880

Google GCM server returns Unauthorized Error 401

I am using GCM services to push information from server.

If I use browser key it shows the success message as :

{"multicast_id":4849013215736515938,"success":1,"failure":0,"canonical_ids":0,"results":
[{"message_id":"0:1348742583011905%2adac3a0f9fd7ecd"}]}

but I did not get any notifications on device.

And if I use server key it shows Unauthorized Error 401. My code is shown below:

$apiKey = "xxxxx";
$registrationIDs = array("xxxxxxxx");
$message = "testing Process";
$url = 'https://android.googleapis.com/gcm/send';
$fields = array(
        'registration_ids'  => $registrationIDs,
        'data'              => array("message"=>$message),
        );
$headers = array( 
        'Authorization: key=' . $apiKey,
        'Content-Type: application/json'
        );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode($fields) );
  
$result = curl_exec($ch);
if(curl_errno($ch)){ echo 'Curl error: ' . curl_error($ch); }
curl_close($ch);
echo $result;

Any ideas?

Upvotes: 31

Views: 73418

Answers (12)

m1uan
m1uan

Reputation: 558

I used to put to the

$apiKey = "xxxxx"

like project number (project id) which i put to Android Client app, but i have been wrong in server have to be appkey from google cloud console, (where you activate Google Cloud Messaging for Android)

    ProjectXXX -> APIs & auth -> Registered Apps -> Web App -> Server Key
 -> Api key

in my case i had there default project 'Service Account-project' on platform: 'Web' but there was just Hosted Application section and no evidence about any api key.

but when i create new Web Apps called 'My app' which contained 4 sections OAuth 2.0 Client ID, Certificate, Server Key, Browser Key i finaly find the api key

Upvotes: 2

Nate Hammond
Nate Hammond

Reputation: 56

Had this trouble, I was using GCM (Google cloud messaging). But after Sep 2016, the server key on the GCM will not work, you have to use FCM (firebase cloud messaging). Create new server keys only in the Firebase Console using the Cloud Messaging tab of the Settings panel. I went over to the firebase console console.firebase.google.com (I had not used it yet) and it asked me to import my project. Suddenly there was this new server key back on the GCM console. If using GCM, use the "legacy key" you see listed there.

Upvotes: 0

Pavan Pyati
Pavan Pyati

Reputation: 960

You must have different project than the default one. Create a project & then create a key. Don't use default project.

Upvotes: 0

Nir
Nir

Reputation: 25369

for me the problem was that you have to enable the api. Having an API set up is not enough. Check that Google Cloud Messaging for Android appears on the enabled APIs in APIs-> enabled APIs.

If its not click the API library tab in APIS and enable it. \

Upvotes: 1

Thomas Decaux
Thomas Decaux

Reputation: 22691

As many people wrote, you have to whitelist your server IPV4 and IPV6. If you want only IPV4, add this to your curl php init:

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

Upvotes: 3

Dan Bartlett
Dan Bartlett

Reputation: 173

I tried everything in this thread, and still no luck.

So I checked enabled APIs (APIs and Auth -> APIs, Enabled APIs), and realised that I had enabled Google Cloud Messaging for Chrome not Google Cloud Messaging for Android. As soon as I enabled the latter, it worked immediately.

Double check you have the right API enabled!

Upvotes: 14

Qin Zhengquan
Qin Zhengquan

Reputation: 467

Here is the problem: I was using 2 servers, 1 staging and the other the production server. For my staging server, which was hosted using digital ocean, I entered the inet address in the Key for server applications allowed IP addresses, and it worked just fine.

However, it failed when I changed to my inet address from my production server. Instead, I had to use inet6 addr: /64 Scope:Global for this to work. To get the value if you encounter the same issue, just enter ifconfig, and find the above entry. Enter that value in the allowed IP addresses and it will work fine.

Upvotes: 0

Lettings Mall
Lettings Mall

Reputation: 300

Looking at the code, I recognise this is from a php-gcm sample posted somewhere online. Its quite nice and I can assure you that both browser keys and whitelisted ip's (not IPV6 or bas64 apikey) work.

The reason why no message is shown is because the send notification function you're using 'data' => array("message"=>$message) targets a key of "message", this is what you must pass to your pending intent i.e

notification.setLatestEventInfo(context, title, message, intent);

This will enable the intent to read the message contained under this key..the message itself is obtained from the GCMIntentService method pasted below:

 @Override
protected void onMessage(Context context, Intent intent) {
    Log.i(TAG, "Received message");
    String message = intent.getExtras().getString("message");
    Log.d(TAG, "The intent contains: " + intent.getExtras());
    displayMessage(context, message);
    // notifies user
    generateNotification(context, message);
}

Upvotes: 0

mayur bhagat
mayur bhagat

Reputation: 209

use browser key using option create new browser key and use this key in your code. https://code.google.com/apis/console/?pli=1#project:42053154xxxx:access

Upvotes: 2

DanO
DanO

Reputation: 10270

Another answer already suggested whitelisting the IP address, which seems kind of obvious. What fixed it for me was whitelisting my IPv6 address. That was the key! I hope this helps someone else.

Upvotes: 19

Jos
Jos

Reputation: 1089

Did you whitelist the IP of your server? This is not necessary by default for the browser key, but it is for the server key.

You can check it here:

https://code.google.com/apis/console/#project:[YOUR PROJECT NUMBER]:access

Upvotes: 32

Case
Case

Reputation: 4282

If I am not mistake your APIKEY needs to be base64 encoded.

Also try var_dump($results) to see if you get information then.

Upvotes: 0

Related Questions