PHP - How to get OAuth 2.0 Token - php

I want to connect to OAuth 2.0 authorization type (with password grant_type) to get token with POST method, it's ok to test from Postman but i couldn't connect through PHP...
here's my information:
request url: 'http://example.com/core/connect/token'
ClientName: 'something1'
ClientId: 'something2'
Secret: 'something3'
UserName: 'something4'
Password: 'something5'
Scope: 'something6'
could you please just give me an example to get token and use?
i've already tested:
$base_url = 'https://example.com/oauth/token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_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, array(
'client_id' => YOUR-CLIENT-ID,
'client_secret' => YOUR-CLIENT-SECRET,
'username' => YOUR-USERNAME-OR-EMAIL,
'password' => YOUR-PASSWORD,
'grant_type' => 'password'
));
$data = curl_exec($ch);
$auth_string = json_decode($data, true);
and this
$api = "KEY GOES HERE";
$authurl = "http://example.com/core/connect/token";
$client_id = "ID GOES HERE";
$client_secret = "SECRET GOES HERE";
// Creating base 64 encoded authkey
$Auth_Key = $client_id.":".$client_secret;
$encoded_Auth_Key=base64_encode($Auth_Key);
$headers = array();
$headers['Authorization'] = "Basic ".$encoded_Auth_Key;
$headers['Content-Type'] = "application/x-www-form-urlencoded";
$data = array(
'grant_type' => 'password',
'scope' => 'read write',
'username' => $api,
'password' => $api,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $authurl);
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
$auth = curl_exec( $ch );
if ( curl_errno( $ch ) ){
echo 'Error: ' . curl_error( $ch );
}
curl_close($ch);
$secret = json_decode($auth);
$access_key = $secret->access_token;
echo $secret;

The first call seems OK for the password grant type. The scope parameter should be optional. So...
Add this to your code to know the response from the server and therefore know the missing parameter or setting.
curl_setopt($ch, CURLOPT_VERBOSE, true);
Check the status code and a possible body with an error message.

Related

PHP CURL BadRequest

I am attempting to send a print job through PrintNode. I keep getting met with the same error message:
code: BadRequest
message: Incorrect request body: (request body).content must be present
I am relatively new to CURL, here is my code:
$pdf_path = plugin_dir_url( __FILE__ ) . 'pdfs/Order8221.pdf';
$url = 'https://api.printnode.com/printjobs';
$data_array = array(
'printerId' => 70029019,
'title' => 'My test print',
'contentType' => 'pdf_uri',
'content' => $pdf_path,
'source' => 'api doc'
);
$data = json_encode($data_array);
echo'<br/>';
print_r($data);
echo '<br/>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, 'API KEY*******' . ":" . '');
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, 'Content-Type: application/json');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$resp = curl_exec($ch);
Any help would be greatly appreciated.
I replaced
$data = json_encode($data_array);
with :
$data = http_build_query($data_array);
and it worked.

Onesignal notification api stopped sending when added Huawei

I have php api for Onesingal notifications to send notification to users when I approve post on my app, it was working perfectly until I added new platform on Onesingal Dashboard which is Huawei, then when I tried to send notification from the app it does't send!
I noticed in the Dashboard that Huawei requires title to send notification! and title already on my api ! but I cann't figure it out!
Notification image
<?php
$title = $_REQUEST["title"];
$sendnotification = $_REQUEST["sendall"];
//here it sends to the post owner that his post is approved
//if ($_SERVER['REQUEST_METHOD'] == 'POST'){
if($isApproved=="1")
{
$ph=$phone;
$arr = $mysqli->query("select * from users where phone='$ph'")->fetch_array(MYSQLI_ASSOC);
$player_id = $arr["token"];
if(!empty($player_id)) {
$content = array(
"en" => "Your post is approved"
);
$heading = array(
"en" => "Your post is approved"
);
$fields = array(
'app_id' => "xxxxxx-xxxxxx-xxxx-xxxx-xxxxxxxxx",
'include_player_ids' => array($player_id),
'contents' => $content
);
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$one2 = curl_exec($ch);
curl_close($ch);
}
}
// here it sends notification fo all users with the post title
if ($isApproved=="1"&& $sendnotification=="1")
{
$content = array(
"en" => $title
);
$heading = array(
"en" => $title
);
$fields = array(
'app_id' => "xxxxxx-xxxxxx-xxxx-xxxx-xxxxxxxxx",
'included_segments' => array('All'),
'contents' => $content
);
$fields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8',
'Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$one2 = curl_exec($ch);
curl_close($ch);
echo $one2;
}
//}
echo json_encode($se);
?>

Invalid Credentials error when trying to add post to blogger using API

I have trying to add a post to blogger using API since yesterday but no luck. Here is my code. When I run it, I get "Invalid Credentials" error.
I have also posted screenshot below which shows from where I am getting that key in Google Developer Console. May be I am picking up wrong key?
Code
<?php
$key = "J7c";
$blog_id = "123456";
$url = 'https://www.googleapis.com/blogger/v3/blogs/'.$blog_id.'/posts/';
$postData = array(
'kind' => 'blogger#post',
'blog' => array('id' => $blog_id),
'title' => 'This is title',
'content' => 'This is content'
);
$data_string = json_encode($postData);
$head = array();
$head[] = 'Authorization: '.$key;
$head[] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $head);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$rsp = curl_exec($ch);
$results = json_decode($rsp);
var_dump($rsp);
?>
Error
Google Developer Console

instagram API responds APISubscriptionError Invalid Response

I have a few subscriptions. Time to time subscription ends and I start the ended subscription again.
Now, my most used subscriptions ended and I can't start again. I'm working in PHP. Nothing has changed in my code, but now I get error while trying to subscribe.
{"meta":{"error_type":"APISubscriptionError","code":400,"error_message":"Invalid response"}}
My other subscriptions are still active but 4 of them are failing. I couldn't fix it.
Edit - Code:
<?php
echo $_GET['hub_challenge'];
$client_id='XXXXX';
$client_secret='XXXXX';
$object = 'tag';
$aspect = 'media';
$object_id = 'XXXXX';
$verify_token='XXXXX';
$callback_url = 'http://XXXXX/callback.php';
$attachment = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'object' => $object,
'object_id' => $object_id,
'aspect' => $aspect,
'verify_token' => $verify_token,
'callback_url'=>$callback_url
);
$url = "https://api.instagram.com/v1/subscriptions/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close ($ch);
print_r($result);
Instagram sends a GET request to uour callback_url and wants you to response with hub.challenge.

Unable to get access token from azure media services REST API using PHP

I have the following code in php:
define("TOKEN_URL", "https://wamsprodglobal001acs.accesscontrol.windows.net/v2/OAuth2-13");
$arrData = array(
'grant_type=client_credentials',
'client_id='.CLIENT_ID,
'client_secret='.urlencode(ACCESS_KEY),
'scope=urn%3aWindowsAzureMediaServices'
);
$arrHeader = array(
'Content-length:'.strlen($this->generateData($arrData))
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, TOKEN_URL);
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->generateData($arrData));
curl_setopt($ch, CURLOPT_HTTPHEADER, $arrHeader);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
$arrToken = json_decode($data);
I am unable to get the token code. Please can anyone check what could be wrong?
There could be a few issues:
You could simplify a few things and use http_build_query():
$data = http_build_query(array(
'grant_type' => 'client_credentials',
'client_id' => CLIENT_ID,
'client_secret' => ACCESS_KEY,
'scope' => 'urn:WindowsAzureMediaServices',
));
$ch = curl_init(TOKEN_URL);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (($res = curl_exec($ch)) === false) {
die(curl_error($ch));
}
$arrToken = json_decode($res);
If there's an error, the first thing to make sure is whether you have an updated list of CA certificates.

Categories