Trustpilot Oauth use token PHP - php

I retrieve the Oauth token in Trustpilot with this script
function get_accesstoken($tp_username,$tp_password,$api_key,$api_secret)
{
$url = 'https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken';
$payloadName = array(
'grant_type' => 'password',
'username' => $tp_username,
'password' => $tp_password
);
$payload = http_build_query($payloadName);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "$api_key:$api_secret");
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$return = json_decode(curl_exec($curl));
print_r($return);
curl_close($curl);
return $return->access_token;
}
now, how can I retrieve the token, verify is exist and use it?
thanks

$url = "https://api.trustpilot.com/v1/oauth/oauth-business-users-for-applications/accesstoken";
$key = "YOUR API KEY";
$secret = "YOUR API SECRET";
$username = 'YOUR LOGIN EMAIL';
$password = 'YOUR LOGIN PASSWORD';
$payload = array(
'grant_type' => 'password',
'username' => $username,
'password' => $password,
);
$authEncodedKeys = base64_encode($key.":".$secret);
$options = array(
'http' => array (
'header' => "Authorization: Basic ".$authEncodedKeys.
"Content-Type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => http_build_query($payload)
)
);
$context = stream_context_create($options);
$results = json_encode(file_get_contents($url, false, $context));
echo $results;
This works for me. Try it.

Related

PHP - How to get OAuth 2.0 Token

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.

Having trouble with authentication to access API

I am trying to retrieve information from an API. The API requires authentication in order to access it. I have the tried the following, but it says that the Authentication has failed. I already confirmed that the credentials are correct, so that is not the problem. Any suggestions?
$pro = '0000000000';
$userName = 'userName';
$password = 'password';
$userPass = $userName.':'.$password;
$host = 'https://wardtlctools.com/wardtrucking/webservices/imaging';
$post = array(
'User' => array(
'UserName' => $userName,
'Password' => $password
),
'Reference' => array(
'RefNo' => $pro,
'RefType' => 'WARDPRO',
'DelMethod' => 'Link',
'Documents' => array(
'DocType' => 'BL',
'DocType' => 'DR'
)
)
);
$ch = curl_init($host);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_USERPWD, $userPass);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close($ch);
print_r($return);
This is the documentation from the provider's website.

curl instead of file_get_contents with context

i would like to replace file_get_contents with curl (sipgate voip account) , but every tryout does not work.
Version mit file_get_contents:
$requestParameter = array(
'RemoteUri' => sprintf('sip:%s#sipgate.de', $remoteUri),
'LocalUri' => sprintf('sip:%s#sipgate.de', $localUri),
'TOS' => 'voice'
);
$auth = base64_encode(sprintf('%s:%s', $username, $password));
$request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);
$context = stream_context_create(
array('http' => array(
'method' => "POST",
'header' => sprintf("Content-Type: text/xml\r\nAuthorization: Basic %s)", $auth),
'content' => $request
))
);
file_get_contents("https://api.sipgate.net/RPC2", false, $context);
Latest Tryouts with curl
$requestParameter = array(
'RemoteUri' => sprintf('sip:%s#sipgate.de', $remoteUri),
'LocalUri' => sprintf('sip:%s#sipgate.de', $localUri),
'TOS' => 'voice'
);
$auth = base64_encode(sprintf('%s:%s', $username, $password));
$request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);
$context = stream_context_create(
array('http' => array(
'method' => "POST",
'header' => sprintf("Content-Type: text/xml\r\nAuthorization: Basic %s)", $auth),
'content' => $request
))
);
$url = 'https://api.sipgate.net/RPC2';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $context);
curl_exec($curl);
curl_close($curl);
AND
$requestParameter = array(
'RemoteUri' => sprintf('sip:%s#sipgate.de', $remoteUri),
'LocalUri' => sprintf('sip:%s#sipgate.de', $localUri),
'TOS' => 'voice'
);
$auth = base64_encode(sprintf('%s:%s', $username, $password));
$request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);
$url = 'https://api.sipgate.net/RPC2';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $auth);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
curl_exec($curl);
curl_close($curl);
Has someone an idea for me?
Thx, spf
The above named error on both versions
$requestParameter = array(
'RemoteUri' => sprintf('sip:%s#sipgate.de', $remoteUri),
'LocalUri' => sprintf('sip:%s#sipgate.de', $localUri),
'TOS' => 'voice'
);
$auth = base64_encode(sprintf('%s:%s', $username, $password));
$request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);
$context = stream_context_create(
array('http' => array(
'method' => "POST",
'header' => sprintf("Content-Type: text/xml\r\nAuthorization: Basic %s)", $auth),
'content' => $request
))
);
$data = http_build_query($context);
$url = 'https://api.sipgate.net/RPC2';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_exec($curl);
curl_close($curl);
and
$requestParameter = array(
'RemoteUri' => sprintf('sip:%s#sipgate.de', $remoteUri),
'LocalUri' => sprintf('sip:%s#sipgate.de', $localUri),
'TOS' => 'voice'
);
$auth = base64_encode(sprintf('%s:%s', $username, $password));
$request = xmlrpc_encode_request("samurai.SessionInitiate", $requestParameter);
$data = http_build_query($request);
$url = 'https://api.sipgate.net/RPC2';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, $auth);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_exec($curl);
curl_close($curl);

Building a URL with JSON data as a query string parameter value

I have this data
$data = array('username' => 'myname',
'password' => 'mypass'
);
$json = json_encode($data);
How can i pass the json as a variable to a url that looks like this
'http://example.com/login/?data='
this is what i have tried and get '400: data not passed' error
$data = array('username' => 'myname',
'password' => 'mypass'
);
$data_string = json_encode($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($curl, CURLOPT_URL, 'http://example.com/login/?data='); //also 'http://example.com/login/'
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json',
'Content-Length: ' . strlen($data_string)
));
curl_setopt($curl, CURLOPT_HEADER, TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
// Exec
$response = curl_exec($curl);
$info = curl_getinfo($curl);
curl_close($curl);
Any data used in the query string should be URL-encoded.
$url = 'http://example.com/login/' . http_build_query([
'data' => json_encode([
'username' => 'myname',
'password' => 'mypass'
])
]);
http_build_query() will take an associative array of parameters, and apply all the proper encodings, building the whole query string for you.
The URL returned:
http://example.com/login/data=%7B%22username%22%3A%22myname%22%2C%22password%22%3A%22mypass%22%7D
This is possible to do this, you are pretty much there.
$data = array('username' => 'myname',
'password' => 'mypass'
);
$json = json_encode($data);
$url = 'http://example.com/login/?data=' . urlencode($json);
This will be a safe url that can do what ever you want with it.

how can i add items in youtubeplaylist?

I am trying to use this code to insert item in youtubeplaylist and its not working as i want ... i think its showing me if playlist contain this id .
please check and let me know what i need to change in code to insert this ID in my playlist .
Here is code :
$option = array(
'part' => 'snippet',
'mine' => 'true',
'key' => 'AIzaSyCJlbT_sqTG2nOUMYU24WzzxXpOelaiYTA',
'access_token' => $_SESSION['info']['access_token'],
'playlistId' => $ID,
'kind' => 'youtube#video',
'videoId' => 'KMGuyGY5gvY',
);
$url = "https://www.googleapis.com/youtube/v3/playlistItems?".http_build_query($option, 'a', '&');
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curlheader[0] = "Authorization: Bearer " . $_SESSION['info']['access_token'];
curl_setopt($curl, CURLOPT_HTTPHEADER, $curlheader);
$json_response = curl_exec($curl);
curl_close($curl);
$responseObj = json_decode($json_response);
thanks in advance.

Categories