How to reproduce OAuth1.0 Request Headers in PHP CURL - php

I am trying to make Twitter verify_credentials request on webserver using PHP 7.4.
I get a http 200 code and proper response only when I set OAuth1.0 Request Headers settings in Postman like that:
Any other way to make request with the same data returns me an error with 401 http status code
{"errors":[{"code":32,"message":"Could not authenticate you."}
I need to convert this Postman settings in PHP CURL of GUZZLE or another http request client code. But when I import CURL examples from Postman, it always throws the same 401 exception. So I tried different ways:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.twitter.com/1.1/account/verify_credentials.json",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: OAuth oauth_consumer_key=\"oauth_consumer_key\",oauth_token=\"oauth_token\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"1605187800\",oauth_nonce=\"hmkiezWh6xqlfJYpK55rDVgcGydQkuBH\",oauth_version=\"1.0\",oauth_callback=\"http%3A%2F%2Fmyurl.com\",oauth_signature=\"signature\""
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Or another one:
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
$client = new Client;
$headers =[
'Authorization' => 'OAuth oauth_consumer_key="oauth_consumer_key",oauth_token="oauth_token",oauth_signature_method="HMAC-SHA1",oauth_timestamp="1605187800",oauth_nonce="hmkiezWh6xqlfJYpK55rDVgcGydQkuBH",oauth_version="1.0",oauth_callback="http%3A%2F%2Furl.com",oauth_signature="SdB60Nr6AhJzOdAIWlW%2FwdmeJM4%3D"',
];
$request = new Request('GET', 'https://api.twitter.com/1.1/account/verify_credentials.json', $headers);
$client->send($request);
$response = $client->getResponse();
echo $response->getBody();
Or that way:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.twitter.com/1.1/account/verify_credentials.json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
$headers = array();
$headers[] = 'Authorization: OAuth oauth_consumer_key=\"oauth_consumer_key\",oauth_token=\"oauth_token\",oauth_signature_method=\"HMAC-SHA1\",oauth_timestamp=\"1605187800\",oauth_nonce=\"hmkiezWh6xqlfJYpK55rDVgcGydQkuBH\",oauth_version=\"1.0\",oauth_callback=\"http%3A%2F%2Furl.com\",oauth_signature=\"H%2FpmcdPUnlMD8RN42RpfBs%2Fs7Cc%3D\"';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
Every time get an 401 error. So, how can I set up all OAuth1.0 properties in PHP CURL to reproduce the same request with the same headers which works in Postman?
P.S. I have already tried abraham/twitteroauth, laravel/socialite and other solutions with the same result

If you are using guzzle 6 or above, then you can directly use guzzlehttp/oauth-subscriber package created by guzzle itself to handle it (otherwise it is a long process),
Add the following to your composer.json:
{
"require": {
"guzzlehttp/oauth-subscriber": "0.4.*"
}
}
I have taken the example from their docs,
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Subscriber\Oauth\Oauth1;
$stack = HandlerStack::create();
$middleware = new Oauth1([
'consumer_key' => 'my_key',
'consumer_secret' => 'my_secret',
'token' => 'my_token',
'token_secret' => 'my_token_secret'
]);
$stack->push($middleware);
$client = new Client([
'base_uri' => 'https://api.twitter.com/1.1/',
'handler' => $stack
]);
// Set the "auth" request option to "oauth" to sign using oauth
$res = $client->get('account/verify_credentials.json', ['auth' => 'oauth']);
You can follow the docs(https://github.com/guzzle/oauth-subscriber) of guzzle/oauth-subscriber for more info.

The cause was not in CURL options but in invalid signature, because it depends on timestamp as I understand it. So I cannot use one signature with different timestamps

Related

Is there a way to get OAuth 2.0 token directly with CURL?

I want to get an access token to call to Google Directory API. I have seen several posts with PHP Curl code, but everytime there has to be a human action to permit access before you get the access token. Is there a way to make a CURL request and get the access token directly?
This is my code so far:
define("CALLBACK_URL", "http://localhost/los/index");
define("AUTH_URL", "https://accounts.google.com/o/oauth2/auth");
define("ACCESS_TOKEN_URL", "https://oauth2.googleapis.com/token");
define("CLIENT_ID", "**.apps.googleusercontent.com");
define("CLIENT_SECRET", "**");
define("SCOPE", "https://www.googleapis.com/auth/admin.directory.device.chromeos");
function getToken(){
$curl = curl_init();
$params = array(
CURLOPT_URL =>
ACCESS_TOKEN_URL."?"."&grant_type=authorization_code"."&client_id=".
CLIENT_ID."&client_secret=". CLIENT_SECRET."&redirect_uri=". CALLBACK_URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_TIMEOUT => 30,
CURLOPT_ENCODING => '',
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_NOBODY => false,
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"Content-Length: 0",
"content-type: application/x-www-form-urlencoded",
"accept: *",
"accept-encoding: gzip, deflate",
),
);
curl_setopt_array($curl, $params);
$response = curl_exec($curl);
$err = curl_error($curl);
echo $response;
curl_close($curl);
if ($err) {
echo "cURL Error #01: " . $err;
} else {
$response = json_decode($response, true);
if(array_key_exists("access_token", $response)) return $response;
if(array_key_exists("error", $response)) echo $response["error_description"];
echo "cURL Error #02: Something went wrong! Please contact admin.";
}
}
When I run it I get this error message:
{ "error": "invalid_request", "error_description": "Missing required parameter: code" }Missing required parameter: codec
I've just been through a similar problem with using PHP cURL to get an access token from the http code returned by the Google API after the user authorizes the consent screen. Here's my long-winded answer:
From least to most important: you should start by fixing the brackets after your if conditions:
if (array_key_exists("access_token", $response)) {
return $response
} elseif (array_key_exists("error", $response)) {
echo $response["error_description"];
echo "cURL Error #02: Something went wrong! Please contact admin.";
}
The error message "Missing required parameter: code" appears because you need to post the code that was returned in the url after the client authorizes your app. You'd do that by doing something like:
CURLOPT_URL => ACCESS_TOKEN_URL."?"."&code=".$_GET['code']."&grant_type=authorization_code"."&client_id=".CLIENT_ID."&client_secret=". CLIENT_SECRET."&redirect_uri=".CALLBACK_URL,
To make it more semantic you'd define the $authorization code variable before that:
$authorizationCode = $_GET['code'];
Lastly, the most important part is that to get an Access Token from Google you need to used the cURL post method as shown in the documentation:
https://developers.google.com/identity/protocols/oauth2/web-server#exchange-authorization-code
You can do that with PHP cURL with this command:
curl_setopt( $ch, CURLOPT_POST, 1);
But then you need to break your URL into two parts: the host and the fields to be posted. To make it easier to read, you can setup your endpoint and the fields into variables:
$endpoint = 'https://oauth2.googleapis.com/token';
$fieldsToPost = array(
// params for the endpoint
'code' => $authorizationCode, //being that $authorizationCode = $_GET['code'];
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SECRET,
'redirect_uri' => CALLBACK_URL,
'grant_type' => 'authorization_code',
);
Then you can use the $enpoint to set the url and http_build_query() to set up your fields. In the end, adapting your code to the code that worked for me would look something like this:
function getToken() {
$endpoint = 'https://oauth2.googleapis.com/token';
$fieldsToPost = array(
// params for the endpoint
'code' => $authorizationCode, //being that $authorizationCode = $_GET['code'];
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SECRET,
'redirect_uri' => CALLBACK_URL,
'grant_type' => 'authorization_code',
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $endpoint);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($fieldsToPost));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE );
// get curl response, json decode it, and close curl
$googleResponse = curl_exec($curl);
$curl_error = curl_errno($curl);
$googleResponse = json_decode( $googleResponse, TRUE );
curl_close( $curl );
return array( // return response data
'url' => $endpoint . '?' . http_build_query( $params ),
'endpoint' => $endpoint,
'params' => $params,
'has_errors' => isset( $googleResponse['error'] ) ? TRUE : FALSE, // b oolean for if an error occured
'error_message' => isset( $googleResponse['error'] ) ? $googleResponse['error']['message'] : '', // error message
'curl_error' => $curl_error, //curl_errno result
'google_response' => $googleResponse // actual response from the call
);
}
To check the response you can use the following print function to see the response:
if (isset($_GET['code'])) {
$response = getToken();
echo '<pre>';
print_r($response);
die();
}
I know it's been a long time and you've probably found a workaround but I hope this is still useful for future projects. Cheers!

How to use dynamic user-agent header using PHP Curl or Guzzel Client

I am new to PHP and trying to call a REST Service. I could do that using either Curl or Guzzle Client in PHP. Later I am calling this from Mozilla and Chrome Browser.
The problem is Guzzle and Curl are not forwarding the actual User-Agent header as Request Header to the backend services.
The default Guzzle User-Agent header is Guzzle/ver curl/ver PHP/ver
I know we can add custom/hardcoded headers in both Curl and Guzzle. But I dont want to hardcode.
<?php
require './vendor/autoload.php';
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'http://sample.com');
$data = json_decode($res->getBody(), true);
//echo $res->getBody()
?>
<html>
<body>
<p>Body is <?php echo $res->getBody() ?> </p>
</body>
</html>
When I call the PHP service from either Chrome/Mozilla/Mobile/Safari, I want the respective user-agent headers to be sent as request headers to backend services.
If there a way to do this in any way?
PHP has a build-in array which stores data from request -$_SERVER['HTTP_USER_AGENT'].
You can then set the user-agent guzzle uses with the headers option.
$client->request('GET', '/get', [
'headers' => [
'User-Agent' => $_SERVER['HTTP_USER_AGENT'],
]
]);
For GuzzleHttp\Client:
$client = new GuzzleHttp\Client([
'headers' => [
'User-Agent' => $_SERVER['HTTP_USER_AGENT'],
]
]);
$res = $client->request('GET', 'http://sample.com');
or
$client = new GuzzleHttp\Client();
$res = $client->request('GET', 'http://sample.com', [
'headers' => [
'User-Agent' => $_SERVER['HTTP_USER_AGENT'],
]
]);
For php cUrl:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://sample.com',
CURLOPT_TIMEOUT => 0,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => $_SERVER['HTTP_USER_AGENT'],
));
curl_setopt($curl, CURLOPT_USERAGENT, $agent);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Hope this helps for someone

curl in PHP to get access_token OAuth2 return error: "invalid_client"

I got a problem when getting access_token from OAuth2 Server. I'm using curl in PHP to get the access_token, but it failed and return:
error: "invalid_client",
error_description: "Client authentication failed."
The strange is, if I use the curl from command prompt:
curl -d "client_id=f3d259ddd3ed8ff3843839b&client_secret=4c7f6f8fa93d59c45502c0ae8c4a95b&redirect_uri=http://application.dev/oauth/handle&grant_type=authorization_code&code=rZCQQBXVSQqKi2IRro1gYkSsRhyUcLsNODACjwPw" http://oauth-server.dev/oauth/access_token
it success and return the access_token:
"access_token":"fI7APDRZygrsF1BiegAQCS1yUT8vnm1LgD5bIu2U",
"token_type":"Bearer",
"expires_in":3600
I'm using Laravel 5.2 and here's my code to handle from OAuth Server to get access_token:
public function getOAuthHandle(Request $request){
$url = 'http://oauth-server.dev/oauth/access_token';
$code = $request->code;
$client_id = 'f3d259ddd3ed8ff3843839b';
$client_secret = '4c7f6f8fa93d59c45502c0ae8c4a95b';
$redirect_uri = 'http://application.dev/oauth/handle';
$clienttoken_post = array(
"code" => $code,
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => $redirect_uri,
"grant_type" => "authorization_code"
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json_response = curl_exec($curl);
curl_close($curl);
return $json_response;
}
I have tried to another script from internet and stackoverflow and restart Apache and PHP, but it still doesn't work.
Is there any way to solve this problem? What should I check?
Thank you for your help and answer.
Try to send the request on curl headers instead.
$curl = \curl_init();
\curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 600,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_HTTPHEADER => $clienttoken_post,
CURLINFO_HEADER_OUT => true,
));
$response = \curl_exec($curl);
Either sent the grant_type on headers and then client_secret and client_id as post, or sent them on headers try it out. And btw. $request->code should be checked before passing to curl request. And all the url, client_secret, client_id MUST be with in .env and get them through config/services.

Set proxy in Guzzle

I have a problem with set proxy in guzzle that a blank page was shown while with curl everything works perfect. The code that I used in guzzle and curl came below.
What is wrong with this code:
Guzzle:
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
require_once "vendor/autoload.php";
try {
$client = new Client();
$request = new \GuzzleHttp\Psr7\Request('GET', 'http://httpbin.org');
$response = $client->send($request, [
'timeout' => 30,
'curl' => [
'CURLOPT_PROXY' => '*.*.*.*',
'CURLOPT_PROXYPORT' => *,
'CURLOPT_PROXYUSERPWD' => '*:*',
],
]);
echo '</pre>';
echo($response->getBody());
exit;
} catch (RequestException $e) {
echo $e->getRequest();
if ($e->hasResponse()) {
echo $e->getResponse();
}
}
And The code with CURL:
$url = 'http://httpbin.org';
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_PROXY, '*.*.*.*');
curl_setopt($ch, CURLOPT_PROXYPORT, *);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, '*:*');
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$page = curl_exec($ch);
echo $page;
Thanks.
As for Guzzle 6.
Guzzle docs give info about setting proxy for a single request
$client->request('GET', '/', ['proxy' => 'tcp://localhost:8125']);
But you can set it to all requests when initializing client
$client = new Client([
'base_uri' => 'http://doma.in/',
'timeout' => 10.0,
'cookie' => true,
'proxy' => 'tcp://12.34.56.78:3128',
]);
UPD. I don't know why, but I face a strange behaviour. One server with guzzle version 6.2.2 works great with config as above, and the other one with the same version receives 400 Bad Request HTTP error from a proxy. It is solved with another config structure (found in docs for guzzle 3)
$client = new Client([
'base_uri' => 'http://doma.in/',
'timeout' => 10.0,
'cookie' => true,
'request.options' => [
'proxy' => 'tcp://12.34.56.78:3128',
],
]);
for a proxy, if you have the username and password, you can use:
$client = new GuzzleHttp\Client();
$res = $client->request("POST", "https://endpoint.com", [
"proxy" => "http://username:password#192.168.16.1:10",
]);
this worked with guzzle in php.
For Guzzle6, I think the best way is to implement a middleware for setting proxy.
From Guzzle6 docs:
request-options.proxy
handlers-and-middleware
We can set proxy as below:
use Psr\Http\Message\RequestInterface;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\Client;
use GuzzleHttp\Middleware;
use Util\Api;
function add_proxy_callback($proxy_callback) {
return function (callable $handler) use ($proxy_callback) {
return function (RequestInterface $request,$options) use ($handler,$proxy_callback) {
$ip = $proxy_callback();
$options['proxy'] = $ip;
return $handler($request,$options);
};
};
}
$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(add_proxy_callback(function() {
return Api::getIp(); //function return a ip
}));
$client = new Client(['handler'=>$stack]);
$response = $client->request('GET','http://httpbin.org/ip');
var_dump((string)$response->getBody());
Had the same problem right now , and all i needed to do was use curl array keys as constants instead of strings ..
$response = $client->send($request, [
'timeout' => 30,
'curl' => [
CURLOPT_PROXY => '*.*.*.*',
CURLOPT_PROXYPORT => *,
CURLOPT_PROXYUSERPWD => '*:*',
],
]);
See CURL options Keys , they are not strings anymore.
The procedure for psr-7 may be different, but if you're using
the standard way to instantiate a client,
path\to\project\vendor\guzzlehttp\guzzle\src\Client.php, lines 164-170 includes code to read the environment variables to see if HTTP_PROXY and HTTPS_PROXY are set on the current machine, and if yes, Guzzle will use those values.
Additionally, I had to set my HTTPS_PROXY = http://ip:port (not https), because our workplace proxy seems to handle both https and http requests via the http protocol.
The advantage of this configuration is that you don't have to chnge proxy settings in your source code.
$response = \Drupal::httpClient()->post($settings['base_url'] . 'api/search/', [
'verify' => true,
'body' => $post_data,
'headers' => [
'Content-type' => 'application/json',
],
'curl' => [
CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1_2,
CURLOPT_PROXY => 'proxyip:58080'],
]
)->getBody()->getContents();
Set proxy/https in Guzzle and SSL its work perfect.

Error to call Tinypass response in Restapi

I am new in Tinypass api integration.
I try to integrate Tinypass API using PHP. Code below:
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://sandbox.tinypass.com/r2/access?rid=portfolio_id&user_ref=badashah26',
// CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_HTTPHEADER => array(
'AID: xxxxxxxx', // PUT your AID
'signature: xxxxxxxxxxxxxxxxxx', // PUT your signature
'sandbox: true'
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
print_r($resp); exit();
Response get but error display.
"error":{"message":"Access denied: invalid AID or signature","code":401}}
Any one can find solutions.
Thanks
Our REST API documentation has been updated to provide a few steps on how to generate your own API header. Check out the documentation at http://developer.tinypass.com/main/restapi.
Best,
Tinypass Support
Something like this..? (untested)
$aid = 'YOUR AID';
$action = '/r2/access?rid='.$rid.'&user_ref='.$userref;
$request = 'GET '.$action;
$url = 'http://sandbox.tinypass.com'.$action;
$signature = hash_hmac('sha256',$request,$aid);
$auth = $aid.':'.$signature;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array('Authorization: '.$auth);
));
$resp = curl_exec($curl);
curl_close($curl);
print_r($resp); exit();

Categories