Reputation: 35
i got a app using fcm to notificantions and all works fine when i send messages via the console, but when i try to send via php(curl post) or postman(firefox extension) i got a auth error(even using the key)
string(304) "{ "error": { "code": 401, "message": "Request is missing required authentication credential. 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" } } "
i got the key from the firebase project console my php code is
public function send_FCM(){
// Method - 1
$fcmUrl = 'https://fcm.googleapis.com/v1/projects/anal.........719/messages:send';
$notification = array(
"to"=>"/topic/cidade396",
"notification"=>array(
"body"=>"test 123"
)
);
$headers = [
'Authorization: key=AIz.........9KWwy5BisxXYbA8',
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$fcmUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($notification));
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
return true;
} anyone know how can i solve this?
Upvotes: 0
Views: 2712
Reputation: 7546
Legacy FCM uses the key you're getting from the Firebase console. With FCM v1, which is what you're using, you use more secure credentials created from a service account key. You download the service account in the Firebase console under Project Settings>Service Accounts and click "Generate new private key". See the Firebase guide for more info. PHP directions for getting credentials are here
The format will be
Authorization: Bearer ya29.ElqKBGN2Ri_Uz...HnS_uNreA
But it will not work for the API Key. You'll need to pass the Google credentials which you can find out how to generate in the link above.
Upvotes: 2
Reputation: 38289
The example in the documentation for authorizing HTTP v1 send requests shows the Authorization header value starts with Bearer
not key=
:
$headers = [
'Authorization: Bearer AIz.........9KWwy5BisxXYbA8',
'Content-Type: application/json'
];
Upvotes: 0