Google Cloud Messaging, returning 401 Unauthorized

2019-05-05 11:53发布

问题:

I followed the example on https://developer.android.com/google/gcm/notifications.html#gen-client trying to create notification key from client.

getAccounts() method did return a valid google account. GoogleAuthUtil.getToken(context, accountname, scope) also returns a valid scope string. However the response from the post on https://android.googleapis.com/gcm/googlenotification is 401, and is an html page:

<HTML>
    <HEAD>
        <TITLE>Unauthorized</TITLE>
    </HEAD>
    <BODY BGCOLOR="#FFFFFF" TEXT="#000000">
        <H1>Unauthorized</H1>
        <H2>Error 401</H2>
    </BODY>
</HTML>

By the way here is the request code I used to generate the notification Key on the client Side

request:
{
   "operation": "add",
   "notification_key_name": "appUser-Chris",
   "registration_ids": ["4"]
   "id_token": "id_token"
}

And concerning the Post Request I have used this URL

 https://android.googleapis.com/gcm/googlenotification

Along with this header information

content-type: "application/json"
Header : "project_id": <projectID>

PS : I also tried to include the Authorization Key to generate the notification key but it didn't work .

Note : Keep in mind that I want to generate the Notif Key on the client side not on the server side.

Wondering whether this is a known issue or anybody has any idea? Thanks! And I would really Appreciate your help.

I have searched on forums as well as in stackoverflow , but I couldn't find any answer.