PayPal REST token request: api.paypal.com OK, api-m fails - paypal

Addendum 2:The Mozilla behavior is specific to which host the URL resolves to; I've added curl script showing this at the end of the Question.
Addendum: This had gone away 8 hours later or so, and worked for several days. But a week later, I re-run the page just to check, and it is failing repeatedly again: api.paypal works, api-m.paypal doesn not.
I receive different results requesting a live-site access token from api-m.paypal.com and api.paypal.com. If I make the request to api.paypal.com, it works and a token is returned. If I request it from api-m.paypal.com, I receive a 403 Forbidden error. How is this possible? In general, and for token requests, the documentation seems to use api and api-m interchangeably. What's the difference between the two and what calls should be routed to api vs api-m? When I'm running my whole store on the sandbox, everything goes to api-m and works fine. In a test program that just requests tokens repeatedly, sequencing through api, api-m, api.sandbox, and api-m.sandbox --- only api-m fails, the other 3 cases are good. I saw an api vs api-m discussion once but can't find it again; pretty sure it didn't mention this!
<?php
include("../_private/ppinfo.php");
header('Content-type: text/plain');
$sandbox = 0;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "\n";
$sandbox = 1;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "\n";
$sandbox = 2;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "\n";
$sandbox = -1;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "\n";
$sandbox = 0;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "\n";
$sandbox = -1;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "\n";
$sandbox = 1;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "\n";
$sandbox = 0;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "\n";
$sandbox = 2;
echo "sandbox $sandbox rv " . GetNewPPToken($sandbox) . "\n";
// Get a paypal REST token to use for the rest of our transactions.
// See https://developer.paypal.com/docs/business/get-started/
function GetNewPPToken($sandbox)
{
global $G, $ppinfo;
$headers = array(
"Accept: application/json",
"Accept-Language: en_US",
"Content-Type: application/x-www-form-urlencoded"
);
if ($sandbox > 1)
{
$clid = $ppinfo['sb_acct'];
$secret = $ppinfo['sb_secr'];
$url = "https://api.sandbox.paypal.com/v1/oauth2/token";
}
else if ($sandbox > 0)
{
$clid = $ppinfo['sb_acct'];
$secret = $ppinfo['sb_secr'];
$url = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
}
else if ($sandbox < 0)
{
$clid = $ppinfo['acct'];
$secret = $ppinfo['secr'];
$url = "https://api.paypal.com/v1/oauth2/token";
}
else
{
$clid = $ppinfo['acct'];
$secret = $ppinfo['secr'];
$url = "https://api-m.paypal.com/v1/oauth2/token";
};
$cvt = "grant_type=client_credentials";
$curl = newPPcurl($url, $cvt, $headers);
curl_setopt($curl, CURLOPT_USERPWD, "$clid:$secret");
$resp = curl_exec($curl);
$err = curl_error($curl) ;
$json = json_decode($resp, true);
if (0)
{
echo "response:\n";
print_r($resp);
echo "err:\n";
print_r($err);
echo "token '" . $ppinfo['token'] . "'\n";
};
$ppinfo['token'] = $json['access_token'];
return ($ppinfo['token'] != '' ? 1 : 0);
}
function newPPcurl($url, $flds, $hdrs)
{
$user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1);
if ($flds != '')
curl_setopt($curl, CURLOPT_POSTFIELDS, $flds);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); // or 2?
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
//curl_setopt($curl, CURLOPT_SSLCERT, $pem); // pem file name
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
if ($hdrs != "")
curl_setopt($curl, CURLOPT_HTTPHEADER, $hdrs);
return $curl;
}
Test output:
sandbox 0 rv 0
sandbox 1 rv 1
sandbox 2 rv 1
sandbox -1 rv 1
sandbox 0 rv 0
sandbox -1 rv 1
sandbox 1 rv 1
sandbox 0 rv 0
sandbox 2 rv 1
Here's some curl commands that show this behavior. When api-m.paypal.com resolves to 184.87.90.6, the token is fetched OK with the Mozilla agent (cmd #1). When the IP resolves to 151.101.1.35, the request fails for Mozilla (cmd#2), passes for curl (cmd#3). Note that you'll have to supply your own id:pwd strings to test.
curl -v https://api-m.paypal.com/v1/oauth2/token \
--user-agent "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" \
--resolve api-m.paypal.com:443:184.87.90.6 \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "<id:pwd>" \
-d "grant_type=client_credentials"
curl -v https://api-m.paypal.com/v1/oauth2/token \
--user-agent "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)" \
--resolve api-m.paypal.com:443:151.101.1.35 \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "<id:pwd>" \
-d "grant_type=client_credentials"
curl -v https://api-m.paypal.com/v1/oauth2/token \
--user-agent "curl/7.55.1" \
--resolve api-m.paypal.com:443:151.101.1.35 \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "<id:pwd>" \
-d "grant_type=client_credentials"

The PayPal REST token request's user-agent must be a curl identifier, such as "curl/7.55.1". Using a Mozilla user agent causes 403 FORBIDDEN on api-m.paypal.com, though it will appear to work on api.paypal.com, api.sandbox.paypal.com, and api-m.sandbox.paypal.com

Related

Keycloak Rest API get all available resources

i am trying to list all resources client have access to. I am unable to figure out how to to make the call. I have used this curl
curl -X GET \
http://$URL/auth/realms/$RELM/authz/resource-server/resource \
-H 'Authorization: Bearer$TOKEN' \
-H 'cache-control: no-cache'
so far but i am getting this response :
{"error":"RESTEASY003210: Could not find resource for full path: http://localhost:8070/auth/realms/argo/authz/resource-server/resource"}
Can someone help me to figure out how i can list all resources and if resource is not in the list to create new one ?
SOLUTION that is implementd:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->keyCloakURL . '/realms/' . $this->relmName . '/protocol/openid-connect/token');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"audience=" . KEYCLOAK_CLIENT_NAME . "&grant_type=urn:ietf:params:oauth:grant-type:uma-ticket&response_include_resource_name=true");
$authorization = "Authorization: Bearer " . $user_token['access_token'];
curl_setopt($ch, CURLOPT_HTTPHEADER, array (
'Content-Type: application/x-www-form-urlencoded',
$authorization
));
$result = curl_exec($ch);
if(curl_errno($ch))
{
echo 'curl error';
return false;
}
$result = json_decode($result, true);
curl_close($ch);
if(isset($result['access_token']) && !empty($result['access_token']))
{
$parts = explode('.', $result['access_token']);
if(!isset($parts[1]))
{
return false;
}
$info = $this->base64UrlDecode($parts[1]);
$info = json_decode($info, true);
$return = array ();
if(isset($info['authorization']['permissions']))
{
$permissions = $info['authorization']['permissions'];
foreach($permissions as $ecahPermission)
{
if(isset($ecahPermission['scopes']))
{
// $scopes = array_map('strtolower', $ecahPermission['scopes']);
$return[$ecahPermission['rsname']] = $ecahPermission['scopes'];
}
}
}
return $return;
}
return false;

Disable Shipping details option in PayPal Checkout

I am using PayPal Checkout in my website using the PayPal Integration Wizard
and I want to disable the shipping details. What changes should I make in which file?
Updated
This is my expresscheckout file and I have posted a portion from the paypalfunctions.php as well.
expresscheckout.php
<?php
require_once ("paypalfunctions.php");
$_SESSION["Payment_Amount"] = $_POST["Payment_Amount"];
// ==================================
// PayPal Express Checkout Module
// ==================================
//'------------------------------------
//' The paymentAmount is the total value of
//' the shopping cart, that was set
//' earlier in a session variable
//' by the shopping cart page
//'------------------------------------
$paymentAmount = $_SESSION["Payment_Amount"];
//'------------------------------------
//' The currencyCodeType and paymentType
//' are set to the selections made on the Integration Assistant
//'------------------------------------
$currencyCodeType = "USD";
$paymentType = "Sale";
//'------------------------------------
//' The returnURL is the location where buyers return to when a
//' payment has been succesfully authorized.
//'
//' This is set to the value entered on the Integration Assistant
//'------------------------------------
$returnURL = "http://localhost/Reg/Components/PayPal/billinghandler.php";
//'------------------------------------
//' The cancelURL is the location buyers are sent to when they hit the
//' cancel button during authorization of payment during the PayPal flow
//'
//' This is set to the value entered on the Integration Assistant
//'------------------------------------
$cancelURL = "http://localhost/Reg/Portal/SecretaryProfile.php";
//'------------------------------------
//' Calls the SetExpressCheckout API call
//'
//' The CallShortcutExpressCheckout function is defined in the file PayPalFunctions.php,
//' it is included at the top of this file.
//'-------------------------------------------------
$resArray = CallShortcutExpressCheckout ($paymentAmount, $currencyCodeType, $paymentType, $returnURL, $cancelURL);
$ack = strtoupper($resArray["ACK"]);
if($ack=="SUCCESS" || $ack=="SUCCESSWITHWARNING")
{
RedirectToPayPal ( $resArray["TOKEN"] );
}
else
{
//Display a user friendly Error on the page using any of the following error information returned by PayPal
$ErrorCode = urldecode($resArray["L_ERRORCODE0"]);
$ErrorShortMsg = urldecode($resArray["L_SHORTMESSAGE0"]);
$ErrorLongMsg = urldecode($resArray["L_LONGMESSAGE0"]);
$ErrorSeverityCode = urldecode($resArray["L_SEVERITYCODE0"]);
echo "SetExpressCheckout API call failed. ";
echo "Detailed Error Message: " . $ErrorLongMsg;
echo "Short Error Message: " . $ErrorShortMsg;
echo "Error Code: " . $ErrorCode;
echo "Error Severity Code: " . $ErrorSeverityCode;
}
?>
a portion from paypalfunctions.php
function hash_call($methodName,$nvpStr)
{
//declaring of global variables
global $API_Endpoint, $version, $API_UserName, $API_Password, $API_Signature;
global $USE_PROXY, $PROXY_HOST, $PROXY_PORT;
global $gv_ApiErrorURL;
global $sBNCode;
//setting the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$API_Endpoint);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
//turning off the server and peer verification(TrustManager Concept).
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST, 1);
//if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
//Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php
if($USE_PROXY)
curl_setopt ($ch, CURLOPT_PROXY, $PROXY_HOST. ":" . $PROXY_PORT);
//NVPRequest for submitting to server
$nvpreq="METHOD=" . urlencode($methodName) . "&VERSION=" . urlencode($version) . "&PWD=" . urlencode($API_Password) . "&USER=" . urlencode($API_UserName) . "&SIGNATURE=" . urlencode($API_Signature) . $nvpStr . "&BUTTONSOURCE=" . urlencode($sBNCode);
//setting the nvpreq as POST FIELD to curl
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
//getting response from server
$response = curl_exec($ch);
//convrting NVPResponse to an Associative Array
$nvpResArray=deformatNVP($response);
$nvpReqArray=deformatNVP($nvpreq);
$_SESSION['nvpReqArray']=$nvpReqArray;
if (curl_errno($ch))
{
// moving to display page to display curl errors
$_SESSION['curl_error_no']=curl_errno($ch) ;
$_SESSION['curl_error_msg']=curl_error($ch);
//Execute the Error handling module to display errors.
}
else
{
//closing the curl
curl_close($ch);
}
return $nvpResArray;
}
Solved. In the hash_call function in the paypalfunctions.php file, the NVPRequest for submitting to server should be updated with passing NOSHIPPING=1 as a parameter as below.
//NVPRequest for submitting to server
$nvpreq="METHOD=" . urlencode($methodName) ."&NOSHIPPING=1" . "&VERSION=" . urlencode($version) . "&PWD=" . urlencode($API_Password) . "&USER=" . urlencode($API_UserName) . "&SIGNATURE=" . urlencode($API_Signature) . $nvpStr . "&BUTTONSOURCE=" . urlencode($sBNCode);

facebook realtime subsciption API error

I'm using this link in order to add new subsciption entry:
https://graph.facebook.com/XXX/subscriptions?access_token=YYY&object=payments&callback_url=http://xxx/rlcallback.php&fields=actions,disputes&verify_token=ZZZ
For some reason, I get error:
{
"error": {
"message": "(#100) Invalid object. object should be url or open graph object id.",
"type": "OAuthException",
"code": 100
}
}
But the object "payments" inside my link is clearly valid. What am I missing here?
Make sure you are using the correct parameters: object, callback_url, fields, verify_token...and of course the access_token.
Also (and that may be the problem in this case), you have to use POST, not GET. You can either use CURL with POST to subscribe to the Realtime API, or you just use one of the SDKs as explained in the Facebook docs: https://developers.facebook.com/docs/graph-api/reference/v2.1/app/subscriptions
Here is one example with CURL:
$appsecretProof = hash_hmac('sha256', FBAPPID . '|' . FBSECRET, FBSECRET);
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'facebook-php');
$postData = 'object=page' .
'&callback_url=' . urlencode('http://yourdomain.com/callback.php') .
'&fields=feed' .
'&verify_token=somethingfancy' .
'&access_token=' . FBAPPID . '|' . FBSECRET .
'&appsecret_proof=' . $appsecretProof;
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/' . FBAPPID . '/subscriptions');
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$curlResult = curl_exec($ch);
you need to specify object, callback_url, fields and verify_token parameters as your curl post parameters
example:
curl -F 'object=user' \
-F 'callback_url=' \
-F 'fields=checkins' \
-F 'verify_token=' \
"https://graph.facebook.com//subscriptions?access_token=

Extract facebook data from access token

I'm trying to use this site https://www.oneall.com/ to add social login to a test site. After setting up the code and the login I still don't know how to extract user data from access token. Here's the link I get:
http://MYACCOUNT.api.oneall.com/socialize/redirect.html?provider_connection_token=ACCESS TOKEN HERE
I get this code by the call back page like this
if ( ! empty ($_POST['connection_token']))
{
echo "Connection token received: ".$_POST['connection_token'];
}
else
{
echo "No connection token received";
}
if ( ! empty ($_POST['connection_token']))
{
$token = $_POST['connection_token'];
$site_subdomain = 'myaccountname';
$site_public_key = 'public key';
$site_private_key = 'private key';
$site_domain = $site_subdomain.'.api.oneall.com';
$resource_uri = 'https://'.$site_domain.'/connections/'.$token .'.json';
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $resource_uri);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_USERPWD, $site_public_key . ":" . $site_private_key);
curl_setopt($curl, CURLOPT_TIMEOUT, 15);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
curl_setopt($curl, CURLOPT_FAILONERROR, 0);
$result_json = curl_exec($curl);
if ($result_json === false)
{
echo 'Curl error: ' . curl_error($curl). '<br />';
echo 'Curl info: ' . curl_getinfo($curl). '<br />';
curl_close($curl);
}
else
{
curl_close($curl);
$json = json_decode ($result_json);
$data = $json->response->result->data;
if ($data->plugin->key == 'social_login')
{
if ($data->plugin->data->status == 'success')
{
$user_token = $data->user->user_token;
$user_id = GetUserIdForUserToken($user_token);
if ($user_id === null)
{
LinkUserTokenToUserId ($user_token, $user_id);
}
else
{
}
}
}
}
}
I need to learn how to extract data now and a little example about extracting the user name by this code.
You can use the access token to get the connection details from oneall using their api
You use the connection_token to get the connection details (including
the user's Facebook profile data).
example
http://MYACCOUNT.api.oneall.com/connections/ACCESS TOKEN HERE.json -->>for json formatted data
oneall docs
Once you get the user's token, you then need to make a API call to Facebook, e.g. https://graph.facebook.com/me/?access_token={$access_token}
If the $access_token is correct, Facebook should return the user's details, including name, username and any other details you've asked for.

check if facebook URL redirected or not?

i want to check if the url's in my database are reaching the facebook page they should or redirected to "www.facebook.com".
this is the code i use:
<?php
$conn = mysql_connect('localhost', 'user', 'pass');
mysql_select_db('database');
?>
<?php
$query = "SELECT data_txt FROM jos_sobi2_fields_data WHERE fieldid=8 ";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$url = $row['data_txt'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
foreach($row as $url) {
curl_setopt($ch, CURLOPT_URL, $url);
$out = curl_exec($ch);
$out = str_replace("\r", "", $out);
$headers_end = strpos($out, "\n\n");
if( $headers_end !== false ) {
$out = substr($out, 0, $headers_end);
}
$headers = explode("\n", $out);
foreach($headers as $header) {
if( substr($header, 0, 10) == "Location: " ) {
$target = substr($header, 10);
echo "[$url] redirects to [$target]<br>";
continue 2;
}
}
echo "[$url] does not redirect<br>";
}
?>
the result is this:
[http://www.facebook.com/shanibakshi.grooming.dogtraining] redirects to [http://www.facebook.com/common/browser.php]
[http://www.facebook.com/shanibakshi.grooming.dogtraining] redirects to [http://www.facebook.com/common/browser.php]
and this url -> http://www.facebook.com/common/browser.php is a facebook page that says my browser is old...probably because of some function in the code.....
anyway all i want to do is to check if the url in my database stays in their place with any redirection.
thanks :)
ronen.
Are you saying that you want to detect redirection, but the problem is you are always getting redirected to browser.php so you get nothing but "false positives"? In that case you probably just need to set the USERAGENT option, something like:
curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 6.2; rv:9.0.1) Gecko/20100101 Firefox/9.0.1');