extract access taken variable from responds from server - php

I am busy sending a HTTP curl request for request token , When i send the below code to get access token , when i receive the responds i would like the access token value and not entire responds.
$params = array(
'username' => 'nuser',
'password' => 'password',
);
$curlSecondHandler = curl_init();
curl_setopt_array($curlSecondHandler, [
CURLOPT_URL => 'https://localhost/api/v3/authentication/usernamepassword',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: multipart/form-data',
'Authorization: Bearer '
],
CURLOPT_POSTFIELDS => $params,
]);
$response2 = curl_exec($curlSecondHandler);
curl_close($curlSecondHandler);
$arr = json_decode($response2);
echo $response2->data -> accessToken ;
I get this error
Trying to get property of non-object

Related

Api payment gateway integration PHP

My boss received a payment API ( it allows customer to pay in many instalments ) and asked me to integrate it to my website
But I'm a developper beginner and I don't really know where to start.
If I understood correctly, I need to get access token first.
The Authorization type is : OAuth 2.0
Header Prefix : Bearer
Grant Type : Client Credientials
Scope : Resource.WRITE, resource.READ
Authorization data to : Request Header
How can I get the access token and refresh it ?
I tried something like this : ( but it isn't working )
<?php
$clientId = 'myclientid';
$clientSecret = 'myclientsecret';
function getToken(){
$curl = curl_init();
$params = [
CURLOPT_URL => 'https://test.auth.alphacredit.be/api/oauth/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_NOBODY => false,
CURLOPT_HTTPHEADER => array(
"content-type: application/json",
"accept: */*",
"accept-encoding: gzip, deflate",
'Authorization: Bearer',
),
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'client_id' => $clientId,
'client_secret' => $clientSecret,
'grant_type' => 'client_credentials',
'scope' => 'read, write',))
];
curl_setopt_array($curl, $params);
$response = curl_exec($curl);
curl_close($curl);
}

trying to post data to IOT device using PHP

I have a thingsuno board. There is one led on the board I want to turn on via a POST request (http integrations) if I do this command in my CLI it works:
curl -i -X POST --data '{"dev_id":"myid","port": 1,"confirmed": false, "payload_raw": "MDA="}' https://integrations.thethingsnetwork.org/ttn-eu/api/v2/down/myapplication/test?key=ttn-account-v2.thekey
But now I want to do it in PHP so I have this code:
<?php
//API Url
$endpoint_url = 'https://integrations.thethingsnetwork.org/ttn-eu/api/v2/down/myapp/test?key=ttn-account-v2.mykey';
$data_to_post = [
'dev_id' => 'mydevice',
'port' => 1,
'confirmed' => false,
'payload_raw' => 'MDA='];
$options = [
CURLOPT_URL => $endpoint_url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data_to_post,`enter code here`
CURLOPT_SSL_VERIFYPEER=> false
];
$curl = curl_init();
curl_setopt_array($curl, $options);
curl_exec($curl);
var_dump(curl_getinfo($curl));
curl_close($curl);
And it only returns HTTP400 badrequest. what am I doing wrong ?
Try to encode the data using json_encode:
$data_to_post = json_encode([
'dev_id' => 'mydevice',
'port' => 1,
'confirmed' => false,
'payload_raw' => 'MDA=']);

Missing parameters when requesting OAUTH token survey monkey v3

I'm trying to obtain my "long lived access token" using CURL/PHP but I'm receiving the error "Missing parameters for client_id, client_secret, code, grant_type, redirect_uri".
The URL I'm calling is where you can clearly see the parameters I'm trying to pass in!
https://api.surveymonkey.net/oauth/token?client_secret='.urlencode($client_secret).'&code='.urlencode($short_token).'&redirect_uri='.urlencode($redirect_url).'&client_id='.urlencode($client_id).'&grant_type=authorization_code
I'm also using the content-type of "application/x-www-form-urlencoded" as per the docs (see below).
My CURL request:
function survey_monkey_curl_request($url, $params=[], $request_type = 'get', $access_token) {
print_r($url);
$ch = curl_init();
$headers = [
"Content-Type: application/x-www-form-urlencoded",
"Authorization: bearer " .$access_token
];
$opts = [
CURLOPT_URL => $url,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
];
if ($request_type == 'post') {
$opts[CURLOPT_POST] = 1;
//$opts[CURLOPT_POSTFIELDS] = json_encode($params);
}
if ($request_type == 'patch') {
$opts[CURLOPT_CUSTOMREQUEST] = "PATCH";
$opts[CURLOPT_POSTFIELDS] = json_encode($params);
}
curl_setopt_array($ch, $opts);
$result = curl_exec($ch);
if ($result === false) {
curl_close($ch);
throw new Exception(curl_error($ch));
}
curl_close($ch);
return $result;
}
Where am I going wrong?
Straight from the documentation it looks like to get the long-lived token you need to post your fields:
//Exchange for long-lived token
curl -i -X POST https://api.surveymonkey.net/oauth/token -d \
"client_secret=YOUR_CLIENT_SECRET \
&code=AUTH_CODE \
&redirect_uri=YOUR_REDIRECT_URI \
&client_id=YOUR_CLIENT_ID \
&grant_type=authorization_code"
https://developer.surveymonkey.com/api/v3/?shell#new-authentication
When you append your parameters onto your url you are sending then as GET request paramters
You need to put your data string into CURL POSTFIELDS and do not json encode
The PHP Answer
<?php
$ch = curl_init();
$data = [
'client_secret' => $YOUR_CLIENT_SECRET,
'code' => $AUTH_CODE,
'redirect_url' => $YOUR_REDIRECT_URI,
'client_id' => $YOUR_CLIENT_ID,
'grant_type' => 'authorization_code'
];//set your data as an array
$headers = [
"Content-Type: application/x-www-form-urlencoded",
"Authorization: bearer " . $access_token
];
$opts = [
CURLOPT_URL => $url,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
];
if ($request_type == 'post') {
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = http_build_query($data);// this will build your data string from the array
}
curl_setopt_array($ch, $opts);
$result = curl_exec($ch);
curl_close($ch);
return $result;

Fitbit Web API error grant type

following is the error reported by api
{"errors":[
{
"errorType": "invalid_request",
"message": "Missing 'grant_type' parameter value."
}
], "success": false}
//curl request to fetch token with use of auth code used in my code
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.fitbit.com/oauth2/token',
CURLOPT_HTTPHEADER => array(
'Authorization:Basic'.base64_encode(FITBIT_CLIENT_ID.':'.FITBIT_CLIENT_SECRET),
'Content-Type: application/x-www-form-urlencoded'
),
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'code' => $auth_code,
'client_id' => FITBIT_CLIENT_ID,
'grant_type' => "authorization_code", //auth code received in url params
'redirect_uri' => 'https://www.example.com/auth/fitbit/success'
)
));
$resp = curl_exec($curl);
curl_close($curl);
i am receiving this error. plz help to identify where could be the error.
Wrap your post field's array with to make sure you are doing the encoding (in case special characters).
CURLOPT_POSTFIELDS => http_build_query (array(
...
))
Try putting a space between Authorization header. Standard one to use Authorization: Basic
If still seeing error then use VERBOSE mode with your curl request. And update your question with that verbose output so that we can help you.

How to post a status update to Sina Weibo via OAuth2?

I'm trying to post a status update to the Chinese microblogging website Sina Weibo, via PHP/cURL and OAuth2.
I'm running into this error:
{"error":"auth
faild!","error_code":21301,"request":"/2/statuses/update.json"}
My PHP:
<?php
$ch = curl_init('https://api.weibo.com/2/statuses/update.json');
$headers = array(
'Authorization: Bearer '.$access_token,
'Content-Type: application/x-www-form-urlencoded');
$postData = array('access_token' => '2.00x123456789', 'status' => 'hello');
curl_setopt_array($ch, array(
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postData
));
$response = curl_exec($ch);
echo $response;
curl_close($ch);
?>
I authorized the app with the OAuth2 scope all and the token is valid.
What could be the reason for the error?
Remove your http header
'Content-Type: application/x-www-form-urlencoded'
Replace post fields with:
CURLOPT_POSTFIELDS => http_build_query($postData)
And now Be Happy!

Categories