I’m currently rebuilding a PHP 5.6 application into Java, which is expected to go beyond June. Until then, I need to ensure that push notifications continue to be sent by migrating them.
The problem is that the ‘Google App Engine PHP 5 Standard Environment Document’, which explains user authentication services using Google OAuth2, terminated on January 30. I have two questions about this:
- How can I receive Google OAuth2 tokens with PHP version 5.6?
- How does the sending method using the FCM HTTP v1 protocol work with PHP 5.6?
Through gpt, I received the following source. However, I am not sure of how to get $refreshToken
, and there is uncertainty about whether this source works correctly.
function get_access_token($clientId, $clientSecret, $refreshToken) {
$url = 'https://accounts.google.com/o/oauth2/token';
$data = [
'client_id' => $clientId,
'client_secret' => $clientSecret,
'refresh_token' => $refreshToken,
'grant_type' => 'refresh_token',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
if(array_key_exists('access_token', $result)) {
return $result['access_token'];
} else {
return null;
}
}
function send_notification_common($tokens, $message) {
$clientId = 'MyClientId';
$clientSecret = 'MyClientSecret';
$url = 'https://fcm.googleapis.com/v1/projects/gymgym-4ecef/messages:send';
$fields = array(
'message' => array(
'token' => $tokens,
'notification' => $message,
'data' => $message
)
);
$headers = array(
'Authorization: Bearer ' . get_access_token($clientId, $clientSecret, $refreshToken),
'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, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
Could you tell me how to use this code or if there are other methods?
Thank you.
- Visit the Google Cloud Console.
- From the project drop-down at the top, create a new project or select an existing one.
- From the sidebar menu, select “APIs & Services” > “Credentials”.
- On the “Credentials” page, click “Create Credentials” > “OAuth client ID”.
- In the “Create OAuth client” screen, select “Web application” and click “Create”.
- In the next screen, you can see the client ID ($clientId) and client secret ($clientSecret).
$refreshToken
https://accounts.google.com/o/oauth2/auth?redirect_uri=http://localhost&response_type=code&client_id=myClientId&access_type=offline&prompt=consent&scope=https://www.googleapis.com/auth/cloud-platform
After this, it was moved to the url http://localhost/?code=4/0adLIrYehJFAe5XYrzWOAvX9gdkRyB5gJk6pyAeXpgUSMbgV8eLCMjhDHHxN-85_YUyx5SA&scope=https://www.googleapis.com/auth/cloud-platform
.
I’m not sure what I should do after this