FCM Push - Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential

Viewed 521

I am trying to send fcm message to specific topic by using cURL command but it says error like

{ "error": { "code": 401, "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "status": "UNAUTHENTICATED" } }

enter image description here

I used server key from firebase console

enter image description here

I followed this FCM Documentation

How to resolve this 401 error.

1 Answers

Actually I think why, this is because you need a Service account that will handle it for you.

So follow the steps here and this will make you generate a new key pair. With this key, you can get the access token from a code (see in python below but the documentation has support for NodeJS / Java) :

def _get_access_token():
  """Retrieve a valid access token that can be used to authorize requests.

  :return: Access token.
  """
  credentials = ServiceAccountCredentials.from_json_keyfile_name(
      'service-account.json', SCOPES)
  access_token_info = credentials.get_access_token()
  return access_token_info.access_tokenmessaging.py

And you just have to print this value, copy-paste it and use it as the Bearer OAuth 2 access token.

Related