Paypal Permission API. GetBasicPersonalData in php - php

I have received "tokenSecret" and "response token".
$apiCred_user = "XXXX.com";
$apiCred_pass = "XXXX";
$accessToken = "XXXXXXXXXXXXXXKl8OlQ";
$tokenSecret = "XXXXXXXXXXXXXsRg6ULs";
$url = "https://api.sandbox.paypal.com/nvp";
$auth = new AuthSignature();
$response = $auth->genSign($apiCred_user ,$apiCred_pass ,$accessToken,$tokenSecret,'POST',$url);
$authString =
"token=".$accessToken.
",signature=".$response['oauth_signature'].
",timestamp=".$response['oauth_timestamp'];
Using this call, the signature is received. My doubt, The given url is used to generate the signautre. The call is correct?
To get result from "GetBasicPersonalData" , I have used this below code
$headers = array(
"X-PAYPAL-REQUEST-DATA-FORMAT:NV",
"X-PAYPAL-APPLICATION-ID: APP-80W284485P519543T",
"X-PP-AUTHORIZATION: " .$authString,
"X-PAYPAL-RESPONSE-DATA-FORMAT:json");
$url_api = "https://svcs.paypal.com/Permissions/GetBasicPersonalData";
$post_array = array(
"attributeList"=>array("attribute"=>"http://axschema.org/contact/email"),
"requestEnvelope.errorLanguage"=>"en_US");
$curl_session = curl_init();
curl_setopt($curl_session, CURLOPT_URL,$url_api );
curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl_session, CURLOPT_POST, 1);
curl_setopt($curl_session, CURLOPT_POSTFIELDS, http_build_query($post_array));
curl_setopt($curl_session, CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curl_session, CURLOPT_TIMEOUT, 10);
//curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, 0);
$response = json_decode(curl_exec($curl_session));
curl_close($curl_session);
The response result is "Error"
{"responseEnvelope":
{"timestamp":"2015-02-05T09:26:53.053-08:00","ack":"Failure","correlationId":
"c74e091e7944e","build":"2210301"},"error":
[{"errorId":"560022","domain":"PLATFORM","subdomain":"Application","severity":"Error","category":"Application","message":"The X-PAYPAL-APPLICATION-ID header contains an invalid value","parameter":["X-PAYPAL-APPLICATION-ID"]}]}
Thanks ! 123456

you forgot to ask for user's permission to get his personal data.
let's suppose you want to ask his permission to issue refunds, your request should look like this:
$data = array(
'scope' => array(
'0' => 'REFUND',
'1' => 'ACCESS_BASIC_PERSONAL_DATA'
),
'callback' => 'http://yoursite.com/page.php', // The success page
'requestEnvelope' => array(
'errorLanguage' => 'en_US' // Language used to display errors
)
);
then you'll get the correct access token and token secret.
if you're still stuck, please ask for the complete code.

Related

Plaid API Error "body could not be parsed as JSON" when creating a link token request

I am receiving a "INVALID_BODY" error with the message "body could not be parsed as JSON" when sending a curl request through php to create a plaid link token.
I have the header and body formatted this way:
$ch=curl_init("https://development.plaid.com/link/token/create");
$username = array(
"client_user_id"=>"cus_L7tpXAO0PXsPsh"
);
$headers = array(
'Content-type: application/json'
);
$data = array(
'client_id'=>'ID',
'secret'=>'SECRET',
'client_name'=>'Plaid App',
'user'=>$username,
'products'=>'auth',
'country_codes'=>'US',
'language'=>'en',
'webhook'=>'https://webhook.sample.com'
);
$hstring = http_build_query($headers);
$string = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$token = curl_exec($ch);
echo $token;
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
?>
There is probably a very obvious formatting issue but I can't see it as is. Appreciate any suggestions or criticisms.
I should also mention building out the POST in Postman also gives invalid body error.
I was getting the same error, but for a different reason. The problem in your code is how you are sending your country codes and products. They are expected to be arrays of strings despite the documentation seeming to say otherwise. Also, I don't think you're sending the data in JSON either... Try this:
$data =[
"client_id" => $plaidID,
"secret" => $plaidSecret,
"client_name" => $clientName,
"user" => [
"client_user_id" => $userID
],
"products" => ["auth"],
"country_codes"=>["US"],
"language" => "en"
];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"https://".$apiMode.".plaid.com/link/token/create");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_HTTPHEADER,["content-type: application/json"]);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode((object)$data,JSON_HEX_APOS | JSON_HEX_QUOT ));
$response = curl_exec($ch);

salesforce rest api invalid session id error when using access token

I am trying to retrieve data from salesforce using the REST api and CURL in PHP.
I perform the authentication request and recieve an 'instance_url' and 'access_token' but after performing a query request using those, i receive an "INVALID_SESSION_ID" error.
my authentication request code:
function get_sf_auth_data() {
$post_data = array(
'grant_type' => 'password',
'client_id' => 'xxxxxxxxxxxxxxxxxxxxxx', //My client id (xxx... for this example)
'client_secret' => '111111111111', // My client secret (111... for this example)
'username' => 'my_user_name',
'password' => 'my_user_password'
);
$headers = array(
'Content-type' => 'application/x-www-form-urlencoded;charset=UTF-8'
);
$curl = curl_init('https://login.salesforce.com/services/oauth2/token');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$response = curl_exec($curl);
curl_close($curl);
// Retrieve and parse response body
$sf_access_data = json_decode($response, true);
echo '*********' . $sf_response_data['instance_url'] . '********';
echo '*********' . $sf_response_data['access_token'] . '********';
return $sf_access_data;
}
My query request code:
function get_user_sfid($sf_access_data, $user_id_number){
$sql = "SELECT Id, Name
FROM Account
WHERE ID__c = '$user_id_number'";
$url = $sf_access_data['instance_url'] . '/services/data/v20.0/query/?q=' . urlencode($sql);
$headers = array(
'Authorization' => 'OAuth ' . $sf_access_data['access_token']
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$json_response = curl_exec($curl);
curl_close($curl);
var_dump($json_response); // This prints out the response where i got the error
$response = json_decode($json_response);
return $response['id'];
}
The response to the query request as is:
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
I have also tried using this guide (http://developer.force.com/cookbook/recipe/interact-with-the-forcecom-rest-api-from-php) as reference but it uses an "authorization_code" authentication and not a password one.
EDIT
The app has full access in salesforce and both api version and authentication data are correct
I found the solution.
I defined my headers array as:
$headers = array(
"Authorization" => "OAuth " . $sf_access_data['access_token']
);
When i should have defined it as:
$headers = array(
"Authorization: OAuth " . $sf_access_data['access_token']
);

Is it possible to send batch notifications with subscriptions ids having different fields?

I have this code for batch:
$ch = curl_init();
$google_gcm_url = 'https://gcm-http.googleapis.com/gcm/send';
$title = 'Title';
$msg = 'Message';
// just sample unrealistic ids
$fields['to'] = array (
'mobileSubscriptionId1',
'mobileSubscriptionId2',
'mobileSubscriptionId3'
);
$fields['notification'] = array (
'title' => $title,
'body' => $msg,
// 'url' => 'some_url1',
'url' => array (
'some_url1',
'some_url2',
'some_url3'
)
);
$fields['priority'] = 'high';
$fields['content_available'] = true;
curl_setopt($ch, CURLOPT_URL, $google_gcm_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
curl_close($ch);
Is it possible to set the subcription "mobileSubscriptionId1" so when they click it it will go to the first url field "some_url1"?
Then "mobileSubscriptionId2" to "some_url2" , then "mobileSubscriptionId3" to "some_url3" .
I've tried my code but it doesn't work.
I don't want to loop through curl because it's really slow.
Realistically I have thousands of ids.
This is currently not possible for GCM (and FCM). If you intend to send uniques payloads for each user, you'll have to send a separate request for each. (See my answer in the possible duplicate post).
#markdwhite's comment seems to be feasible.

Google Calendar API with refresh token

My question relates to my previous question where I thought I had solved the issue. I've been able to obtain a refresh_token using scripts I found - including the ones in my previous question.
However now armed with this refresh token I am now trying to do some simple things in the first instance list the events in my test calendar
My problem is that trying to list the events using this URL returns an error - trying to get property of non object. This relates to this line of code:
$cAccessToken = $oToken->access_token;
So I assume there's something wrong in getting the access token.
In the part of my script where I obtained the code I needed to add this code:
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
This produced the token- so I added this line to the code getting the access token again but it simply produced a blank page.
I must admit to be very confused with the google API. I tried using the google-client-api code but don't quite get this either - I keep running into brick walls.
Here's my code:
<?php
$cPublisherID = '';
$cScope = 'https://www.google.com/calendar/feeds/';
$cClientID = 'XXXXXX.apps.googleusercontent.com';
$cClientSecret = 'XXXXXX';
$cRedirectURI = 'XXXXXX';
$cAuthCode = 'XXXXXX';
$cRefreshToken = 'XXXXXX';
$bRefresh = true;
if (empty($cAuthCode)) {
$rsParams = array(
'response_type' => 'code',
'client_id' => $cClientID,
'redirect_uri' => $cRedirectURI,
'scope' => $cScope
);
$cOauthURL = 'https://accounts.google.com/o/oauth2/auth?' . http_build_query($rsParams);
echo("Go to\n$cOauthURL\nand enter the given value into \$cAuthCode\n");
exit();
} // ends if (empty($cAuthCode))
elseif (empty($cRefreshToken)) {
$cTokenURL = 'https://accounts.google.com/o/oauth2/token';
$rsPostData = array(
'code' => $cAuthCode,
'client_id' => $cClientID,
'client_secret' => $cClientSecret,
'redirect_uri' => $cRedirectURI,
'grant_type' => 'authorization_code',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cTokenURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $rsPostData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$cTokenReturn = curl_exec($ch);
$oToken = json_decode($cTokenReturn);
echo("Enter the following values:\n\n");
echo("\$cRefreshToken = '" . $oToken->refresh_token . "';\n");
} // ends
else {
// Get a new Access Token
$cTokenURL = 'https://accounts.google.com/o/oauth2/token';
$rsPostData = array(
'client_secret' => $cClientSecret,
'grant_type' => 'refresh_token',
'refresh_token' => $cRefreshToken,
'client_id' => $cClientID
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $cTokenURL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $rsPostData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$cTokenReturn = curl_exec($ch);
$oToken = json_decode($cTokenReturn);
$cAccessToken = $oToken->access_token;
// Get the results
//$cGANURL = 'https://www.googleapis.com/gan/v1beta1/publishers/' . $cPublisherID . '/events';
$rest_url = 'https://www.googleapis.com/calendar/v3/calendars/primary/events';
$cAuthHeader = "Authorization: OAuth " . $cAccessToken;
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array($cAuthHeader));
curl_setopt($ch, CURLOPT_URL, $rest_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$cJsonReturn = curl_exec($ch);
print_r(json_decode($cJsonReturn));
} // ends else from <all authenticated>
?>
I really don't know enough to resolve this - so basically I think I'm getting the tokens I need including the refresh token but not sure how to test and understand what's going on.
I've used the oauth playground and can get results there but not with my own code. I used different codes by the way as I assumed they should be different?

Submit form on one server, process it and then post results to another domain

I have a form on one page with the action set to /process.php
Within the process.php I have the validation of the form and also it writes to a database on the server it sits on.
What I'd like to do after it has written to the database, is post the variables to another domain that will then process those variables differently.
Example using curl:
$name = 'Test';
$email = 'test#gmail.com';
$ch = curl_init(); // initialize curl handle
$url = "http://domain2.com/process.php";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // times out after 10s
curl_setopt($ch, CURLOPT_POST, 1); // set POST method
curl_setopt($ch, CURLOPT_POSTFIELDS, "text=$name&name=$email"); // post fields
$data = curl_exec($ch); // run the whole process
curl_close($ch);
Example without curl: http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/
you can use file_get_contents for the same.
Example:
$name="foobar";
$messge="blabla";
$postdata = http_build_query(
array(
'name' => $name,
'message' => $message
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
$result = #file_get_contents('http://yourdomain.com/process_request.php', false, $context);
if($http_response_header[0]=="HTTP/1.1 404 Not Found"):
echo "iam 404";
elseif($http_response_header[0]=="HTTP/1.1 200 OK"):
echo "iam 200";
else:
echo "unknown error";
endif;

Categories