ToothlessGear / node-gcm

A NodeJS wrapper library port to send data to Android devices via Google Cloud Messaging
https://github.com/ToothlessGear/node-gcm
Other
1.3k stars 208 forks source link

Error 401 #280

Closed ankitjainOfficial closed 7 years ago

ankitjainOfficial commented 7 years ago

Hello,

I went through issues #227 #151 and other related issues.

I see people are talking about Browser Key and Server Keys.

From my Google console, I see that there are only two options, which can relate to my work. That is, API Key & Service Account Key.

When I use API Key, I see the following error in console node-gcm Unauthorized (401). Check that your API token is correct. +0ms node-gcm Error 4xx -- no use retrying. Something is wrong with the request (probably authentication?) +0ms

Can somebody please guide me, where I went wrong?

Also, when I see Google Console, I see there are no logged request. I don't know, if it is related or not.

Thanks.

eladnava commented 7 years ago

@ankitjainOfficial Server key and browser key have both been merged into API key in the Google Developers Console.

Make sure that you have enabled the Google Cloud Messaging service in your Google developer account for that API key.

ankitjainOfficial commented 7 years ago

Yes, I kinda knew that. My point was, 401 was being returned with the given API key.

eladnava commented 7 years ago

Can you post your code please?

ankitjainOfficial commented 7 years ago

` var gcm = require('node-gcm'); var sender = new gcm.Sender('AIzaSyB5CIl----okcnFt6T4ZmXd0g');

    var notify_message = new gcm.Message({
        collapseKey: 'demo',
        priority: 'high',
        contentAvailable: true,
        delayWhileIdle: true,
        timeToLive: 3,
        restrictedPackageName: "somePackageName",
        dryRun: true,
        data: {
            key1: 'message1',
            key2: 'message2'
        },
        notification: {
            title: "Hello, World",
            icon: "ic_launcher",
            body: "This is a notification that will be displayed ASAP."
        }
    });

    var registrationTokens = ['eIMPY3m6iqo:APA91bGU6uQBAPc3N44kKe5tCqE4QMsFOU7FL4nbqJgoQVo-vUOF46MyhRTjWSYhi93sbstyWBE4ZZ-f9OSXNUWdQpYLIqb07jd2wbwpdjrYZoI3JFjaKqtSxOx58t7tdoFVgR5czIyZ'];
    console.log('Sending notification', sender);
    sender.send(notify_message, { registrationTokens: registrationTokens }, 10, function (err, response) {
      if(err) console.error('notification error: ', err);
      else    console.log('notification response: ', response);
    });`
ankitjainOfficial commented 7 years ago

The code is straight out of the docs. Also, I've double checked the API Key. And removed all restrictions for now as well. Just in case this interests you, Screenshot of GAPI Console.

eladnava commented 7 years ago

Might not want to post your Secret API Key though. I trimmed it out for you.

Interesting, it appears that you can no longer create Server API Keys for GCM via the Google Developers Console, you have to do it through Firebase: http://stackoverflow.com/a/39955137/1123355

ankitjainOfficial commented 7 years ago

@eladnava Thanks, I thought of changing the key, rather than obfuscating. Still, will keep in mind. Also, I have moved to FCM, And now stuck with this error.

{ multicast_id: -1, success: 0, failure: 1, canonical_ids: 0, results: [ { error: 'InvalidPackageName' } ] }

Any help would be appreciated. :)

eladnava commented 7 years ago

Are you setting the restricted_package_name parameter when sending notifications?

Invalid Package Name: A message was addressed to a registration ID whose package name did not match the value passed in the request. Happens when error code is InvalidPackageName.

ankitjainOfficial commented 7 years ago

Thanks a ton @eladnava. Finally, saw the notification on phone. :) 💯