IPP CustomerAgg 401 on getAccountTransactions - intuit-partner-platform

The user flow I'm building for my application is that users can click on myAccounts and I'll display the getCustomerAccounts Results. That works perfectly. Then for each account I have a hyperlink called get transactions. That takes the user to a new page that should list out the transactions for that account. For some reason I'm always getting a 401 Code:ApplicationAuthenticationFailed when I call getAccountTransactions even though the previous call of getCustomerAccounts worked fine.
I'm confused as I imagine the authentication that is failing for the 401 is the exact same that works for the earlier call. Here is my code:
function get_transactions($accountID)
{
IntuitAggCatHelpers::GetOAuthTokens( $oauth_token, $oauth_token_secret);
$signatures = array( 'consumer_key' => OAUTH_CONSUMER_KEY,
'shared_secret' => OAUTH_SHARED_SECRET,
'oauth_token' => $oauth_token,
'oauth_secret' => $oauth_token_secret);
$txnStartDate = '2014-06-01'; // YYYY-MM-DD
$url = FINANCIAL_FEED_URL ."v1/accounts/$accountID/transactions?txnStartDate=$txnStartDate";
$action = 'GET';
$oauthObject = new OAuthSimple();
$oauthObject->setAction( $action );
$oauthObject->reset();
$result = $oauthObject->sign(
array
(
'path' => $url,
'parameters'=>
array
(
'oauth_signature_method' => 'HMAC-SHA1',
'Host' => FINANCIAL_FEED_HOST
),
'signatures'=> $signatures
)
);
$options = array();
$curlError = fopen('php://temp', 'rw+');
$options[CURLOPT_STDERR] = $curlError;
$options[CURLOPT_CUSTOMREQUEST] = $action;
$options[CURLOPT_URL] = $result['signed_url'];
$options[CURLOPT_HEADER] = 1;
$options[CURLOPT_VERBOSE] = 1;
$options[CURLOPT_RETURNTRANSFER] = 1;
$options[CURLOPT_SSL_VERIFYPEER] = true;
$options[CURLOPT_HTTPHEADER] = array
(
'Accept:application/json',
'Content-Type:application/json',
//'Content-Length:' . strlen( $postData ),
'Host:'. FINANCIAL_FEED_HOST,
//'Authorization:' . $result['header']
);
$curlError = fopen('php://temp', 'rw+');
$options[CURLOPT_STDERR] = $curlError;
$ch = curl_init();
curl_setopt_array( $ch, $options );
$responseText = urldecode( curl_exec( $ch ) );
echo $responseText;
//display curl http conversation
rewind( $curlError );
stream_get_contents( $curlError );
fclose( $curlError );
$httpCode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
curl_close( $ch );
return $responseText;
}

Using same certificate(.p12) please try the above getAccountTransaction call from APIExplorer tool.
https://developer.intuit.com/apiexplorer?apiname=CustomerAccountData
Usage Ref - https://developer.intuit.com/docs/0020_customeraccountdata/007_firstrequest
If that works well, then compare the request header and URL with the same from your above code. 401 suggests authentication error which comes when your OAuth header is not properly formed/incorrect. Above comparison should sort this out.
PN - While uploading .p12 file(SAML) in ApiExplorer, sometiems, I get the following error msg.
"Your certificate is invalid. Please use base64 encoded CER format and make sure that the file is not empty". If you get the same then this can't be tested in ApiExplorer.
Thanks

This worked for me:
$result = $oauthObject->sign(array(
'path' => FINANCIAL_FEED_URL . 'v1/accounts/400037865348',
'parameters'=> array('oauth_signature_method' => 'HMAC-SHA1',
'Host'=> FINANCIAL_FEED_HOST,
'txnStartDate' => '2014-01-01',
'txnEndDate' => '2014-08-29'),
'signatures'=> $signatures));
Found at https://intuitpartnerplatform.lc.intuit.com/questions/797819-how-to-get-txnstartdate-into-my-url-without-breaking-oauth-authentication.

Related

Insert post from other site using wp_insert_post

I created a cron that will insert posts from the other site using the rest route and wp_insert_post. On the first cron run, the posts were inserted but when I added a new post from the source site and run the cron again, the new post is not added. I'm not sure what I'm doing wrong. If you could help me with this I would really appreciate it. Please see my code below.
add_action('create_posts_cron', function(){
$post_data = wp_remote_get( ' ' ); // inserted the rest route inside ''
$post_data_decode = json_decode( $post_data['body'] );
foreach ( $post_data_decode as $item ) {
$post_title = $item->title;
$customfield = $item->customfield;
$post_arr = array(
'post_title' => $post_title,
'post_status' => 'publish',
'post_type' => 'custompost',
);
if( !get_page_by_title( $post_title, OBJECT, 'custompost' ) ) {
$post_id = wp_insert_post( $post_arr );
update_field( 'customfield', $customfield, $post_id );
}
}
});

How to Retrieve HTTP Status Code with Guzzle?

New to Guzzle/Http.
I have a API rest url login that answer with 401 code if not authorized, or 400 if missing values.
I would get the http status code to check if there is some issues, but cannot have only the code (integer or string).
This is my piece of code, I did use instruction here ( http://docs.guzzlephp.org/en/stable/quickstart.html#exceptions )
namespace controllers;
use GuzzleHttp\Psr7;
use GuzzleHttp\Exception\ClientException;
$client = new \GuzzleHttp\Client();
$url = $this->getBaseDomain().'/api/v1/login';
try {
$res = $client->request('POST', $url, [
'form_params' => [
'username' => 'abc',
'password' => '123'
]
]);
} catch (ClientException $e) {
//echo Psr7\str($e->getRequest());
echo Psr7\str($e->getResponse());
}
You can use the getStatusCode function.
$response = $client->request('GET', $url);
$statusCode = $response->getStatusCode();
Note: If your URL redirects to some other URL then you need to set false value for allow_redirects property to be able to detect initial status code for parent URL.
// On client creation
$client = new GuzzleHttp\Client([
'allow_redirects' => false
]);
// Using with request function
$client->request('GET', '/url/with/redirect', ['allow_redirects' => false]);
If you want to check status code in catch block, then you need to use $exception->getCode()
More about responses
More about allow_redirects
you can also use this code :
$client = new \GuzzleHttp\Client(['base_uri' 'http://...', 'http_errors' => false]);
hope help you

Getting token works, but making payment not

I'm making a class to make a payment with paypal.
Getting a token via the function getToken() works and it spits out a good response (according to the documentation).
However, when I'm using that token to build up a payment with the same sendAPICall() used for the token; it only returns a empty string (according to var_dump() ).
If I copy the exact curl command from the api-information and paste the beare-token; it works. So something is wrong with my API call....
Clearly I'm missing something. Could anyone point me to the error?
The function which gets the Bearer-token:
public function getToken(){
$headers = array(
"Accept-Language" => 'en_US',
"Accept" => "application/json"
);
$t = json_decode($this->sendAPICall('grant_type=client_credentials', '/oauth2/token', $headers, true));
if($t->error){
$this->error = $t->error_description;
$this->token = NULL;
return false;
}else{
$this->token = $t;
return true;
}
}
The function which should make the payment after checking there is a token available.
public function makePayment(){
$this->getToken();
if($this->error){
return false;
}else{
$d = '{"intent":"sale",
"redirect_urls":{
"return_url":"'.$this->config['returnURL'].'",
"cancel_url":"'.$this->config['cancelURL'].'"
},
"payer":{
"payment_method":"paypal"
},
"transactions":[
{
"amount":{
"total":"'.$this->amount.'",
"currency":"'.$this->config['currency'].'"
},
"description":"'.$this->description.'"
}
]
}';
$headers = array( "Authorization" => $this->token->token_type . ' ' . $this->token->access_token,
"Content-type" => "application/json"
);
return $this->sendAPICall(urlencode($d), '/payments/payment', $headers, false);
}
}
And off course the connection with the paypal API, where I'm using the $auth boolean to make the difference between sending the userpwd or using the token:
private function sendAPICall($data, $url, $headers, $auth=true){
$ch = curl_init();
$options = array( CURLOPT_URL => $this->config['endpoint'].$url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_RETURNTRANSFER => true
);
if($auth){
$options[CURLOPT_USERPWD] = $this->config['client_id'].':'.$this->config['client_secret'];
};
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
return curl_exec($ch);
}
It does not look like this code snippet is passing HTTP headers correctly. CURLOPT_HTTPHEADER takes a unideimensional array with values of the form "headername: value". You need
$headers = array(
"Authorization: " . $this->token->token_type . ' ' . $this->token->access_token,
"Content-type: application/json"
);
Also consider
Checking for curl_errno($ch) / curl_error($ch) and the HTTP response code (curl_getinfo($ch, CURLINFO_HTTP_CODE)) to see if the call was successful.
Creating the request data as an associative array and using json_encode($data) when calling sendAPICall(). This is far easier than manipulating JSON strings manually.

sugarcrm retrieve accounts list in a web page using webservice

I would like to retrieve all accounts datas and contact datas in Sugarcrm database using webservice .
I would like it to show it in a webpage .
How can i go about it?
Modify the below code to your requirements. It uses the SOAP API of SugarCRM.
$user_name = 'admin';
$user_password = 'admin';
$sugarcrm_url = 'http://example.com/'
$options = array(
"uri" => $sugarcrm_url,
"trace" => true
);
$offset = 0;
$limit = 100;
try {
$client = new SoapClient($sugarcrm_url.'service/v4_1/soap.php?wsdl', $options);
$response = $client->__soapCall('login',array(
'user_auth'=>array(
'user_name'=>$user_name,
'password'=>md5($user_password),
'version'=>'.01'
),
'application_name'=>'SoapTest'
));
$session_id = $response->id;
$response = $client->__soapCall('get_entry_list',array(
'session'=>$session_id,
'module_name'=>'Contacts',
'query'=>'',
'order_by'=>'contacts.last_name asc',
'offset'=>$offset,
'select_fields'=>array(),
'max_results'=>$limit)
);
print_r($response);
$response = $client->__soapCall('get_entry_list',array(
'session'=>$session_id,
'module_name'=>'Accounts',
'query'=>'',
'order_by'=>'accounts.name asc',
'offset'=>$offset,
'select_fields'=>array('id', 'name', 'email1'),
'max_results'=>$limit)
);
print_r($response);
$response = $client->__soapCall('logout',array('session'=>$session_id));
} catch (Exception $ex) {
echo $ex->getMessage();
}

Getting Response Body using Zend_http_Client

I am succesfully calling a REST API with the following code
$client = new Zend_Http_Client();
$client->setMethod(Zend_Http_Client::POST);
$client->setUri('http://www.example.com/api/type/');
$client->setParameterPost(array(
'useremail' => '******#*****.***',
'apikey' => 'secretkey',
'description' => 'TEST WEB API',
'amount' => '5000.00'
));
However I would like to get both the header value-(201) and Response Body that are returned after the execution.
How do I proceed with that?
I am assuming that you're actually executing the request via:
$response = $client->request();
At that point all you need is in the $response object,
//Dump headers
print_r($response->headers);
//Dump body
echo $response->getBody();
Refer to the Zend_Http_Response docs at:
http://framework.zend.com/apidoc/1.10/
for more methods that are available.
this should work...
$client->setUri ( $image_source_urls );
$response = $client->request ( 'GET' );
$folder_content = $response->getBody ();