Showing Unauthorized in box api response - php

I am using the box api as mentioned in the documentation. I get the ticket from api_key and then auth_token from api_key and ticket.
But when i use this auth_token for getting information like folder list or file contents etc. I get unauthorized error in response.
Here is my code:
$auth_token = $_REQUEST["auth_token"];
//exit($auth_token);
$headers = array(
"Authorization: BoxAuth api_key=".$box_api_key."&auth_token=".$auth_token
);
$http_message = curl_init("https://www.box.com/api/2.0/folders/0");
curl_setopt($http_message, CURLOPT_RETURNTRANSFER, true);
curl_setopt($http_message, CURLOPT_HTTP_VERSION, "CURL_HTTP_VERSION_1_1");
curl_setopt($http_message, CURLOPT_HTTPHEADER, $headers);
curl_setopt($http_message, CURLINFO_HEADER, true);
curl_setopt($http_message, CURLINFO_HEADER_OUT, true);
curl_setopt($http_message, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($http_message);
print($response);
What may be the problem in the code?
Thanks!
Thank you very much in advance!!!

Make sure you're setting $box_api_key to the api_key you're using to get the ticket and auth token. Also, while this should have no bearing on your question, please use the hostname api.box.com for all API V2 requests (aka, https://api.box.com/2.0/folders/0)

Related

Can't authorize my bot user in discord API with curl

I'm try to send PATCH request with curl to give for user role.
$curl = curl_init("https://discordapp.com/api/v8/guilds/935503210934833192/members/" . $userid);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
$data2 = array(
"roles" => 935504716505436200,
);
$data2 = json_encode($data2);
$headers2 = ["Authorization: Bot token", "Content-Type: application/json"];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers2);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data2);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH");
$role_result = curl_exec($curl);
curl_close($curl);
die(var_dump($role_result));
#header("Location: user.php");
But always get this error string(43) "{"message": "401: Unauthorized", "code": 0}", what i'm can do wrong? I'm sure token is correct.
So, as I can see you are trying to PATCH a guild member, but you are not calling the right API URL.
/guilds/{guild.id}/members/{user.id} - as you can see, you need to provide the user's ID you want to PATCH.
So, as I can see you are trying to PATCH a guild member, but you are not calling the right API URL.
/guilds/{guild.id}/members/{user.id} - as you can see, you need to provide the user's ID you want to PATCH.

CS-CART How to Create new Categories though API

I would like to create new categories in CS-Cart through its API.
So far I have this code (running it through browser, just for testing):
$cfg = get_config(); //connection to DB
$product_data = array();
$product_data["category"] = "Category Test API";
$product_data["company_id"] = 1;
$product_data["status"] = "A";
//CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_USERPWD, 'USER EMAIL'.":".'YOUR API:KEY');
curl_setopt($ch, CURLOPT_URL, $cfg["cscart_store_url"]."api/categories/");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json'
)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($product_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec($ch);
if( !curl_error($ch) ) {
echo "no error";
curl_close($ch);
return json_decode($server_output, TRUE);
}
else {
echo "error found!!!";
print_r("Error: ".curl_error($ch));
return 0;
}
There is a documentation here: https://docs.cs-cart.com/latest/developer_guide/api/entities/categories.html.
But I still couldn't make it work although I did not get any errors from curl_exec.
Your json is well encoded, your header have the Content-Type, i think your problem is your cscart_store_url who have a wrong url or your USERPWD isn't good (I believe you didnt put USER EMAIL in your auth
As long as I understood, cs-cart API has some required values in order to make an INSERT API call. Thus, the categories should be already be created inside cs-cart dashboard and match them somehow in the PHP script
On the other hand, if you want to UPDAtE a product for example, you could UPDAte only the specified values you send to API call, without the need of sending all the required API values.
Last but not least, there are more than one Authorizations methods to make the call. I used CURL in order to do the API call and there is a parameter CURLOPT_HTTPHEADER which I have added the below code:
...
CURLOPT_HTTPHEADER => array(
"Authorization: Basic XXXxxxxXXXXxxxXXXlzLmNvbS5ncjpKejMXXXxxxxXN0Y2MDQxenprbEXXXxxXXXE3Mg==",
"Content-Type: application/json",
"cache-control: no-cache"
),
...
Where Authorization is: Basic base64(user#example.com:API_KEY_taken_from_cscart)
Applying all the above the code and the API worked!
thanks everyone for the help and comments

How to retrieve Mailchimp campaign id using api v3

I'm new to the Mailchimp api v3.0 (using php). I've created a campaign with the api and want to retrieve the campaign id from the cURL return data but can't seem to extract just the id. I've been away from php for awhile so I guess I'm just being brain dead. Clearly, I can print the entire result but just need to extract the campaign id in order to add/update content. Most of the test code follows. How do I extract just the id from $retval?
$json_data = json_encode($options);
$auth = base64_encode('prcAdmin:'. $apikey);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://usn.api.mailchimp.com /3.0/campaigns');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content_Type: application/json',
'Authorization: Basic ' . $auth));
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
$retval = curl_exec($ch);
A few typos in your code, let's fix them first.
An extra space in the url
Content_Type to Content-Type. Good to correct it, though the endpoint returns data in a JSON format.
Assuming that you have a valid Auth with valid campaign data, it should return campaign data in JSON format. To extract the campaign id is pretty easy.
$campaign = json_decode($retval, true);
$campaignId = $campaign['id'];
If you can't get correct a campaign id, try to print out the result by using print_r($retval);. The correct data should look similar to an example response found on this page - http://developer.mailchimp.com/documentation/mailchimp/reference/campaigns/. (From and between curly brackets).
I resolved the problem: I added a:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
and it obviously returned the response differently so the "id" tag could be referenced as:
$id = json_decode($retval);
echo $id->id;
Now it extracts just the "id".

Laravel OAuth Authorization Header for FitBit API

I have been struggling for days now to find a decent solution for Laravel but to no avail.
There are many libraries out there that at one point may have worked to provide a Laravel - FitBit API OAuth integration however after trying over 15 different ones and none of them working I am stuck.
Reading the FitBit Documentation I see that once you receive a token you must swap the authorization code with an access token. To do this you need to send an authorization header like this:
POST https://api.fitbit.com/oauth2/token
Authorization: Basic Y2xpZW50X2lkOmNsaWVudCBzZWNyZXQ=
Content-Type: application/x-www-form-urlencoded
client_id=22942C&grant_type=authorization_code&redirect_uri=http%3A%2F%2Fexample.com%2Fcallback&code=1234567890
I have tried using guzzle and a few other libraries for sending the requests but none of them support the format that FitBit require.
I've seen sites with FitBit API integrated so there must be a solution for this.
If anyone has managed to integrate the FitBit API please let me know where I am going wrong.
Thanks.
I don't have a fitbit account, so I can't test this and it will probably need some tweaking, but I would start with something like:
class FitbitConnection{
public function getToken($request_url, $client_id, $client_secret, $code, $redirect_uri){
// base64 encode the client_id and client_secret
$auth = base64_encode("{$client_id}:{$client_secret}");
// urlencode the redirect_url
$redirect_uri = urlencode($redirect_uri);
$request_url .= "?client_id={$client_id}&grant_type=authorization_code&redirect_uri={$redirect_uri}&code={$code}";
// Set the headers
$headers = [
"Authorization: Basic {$auth}",
"Content-Type: application/x-www-form-urlencoded",
];
// Initiate curl session
$ch = curl_init();
// Set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Options (see: http://php.net/manual/en/function.curl-setopt.php)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_POST, 1);
// Execute the curl request and get the response
$response = curl_exec($ch);
// Throw an exception if there was an error with curl
if($response === false){
throw new Exception(curl_error($ch), curl_errno($ch));
}
// Get the body of the response
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$responseBody = substr($response, $header_size);
// Close curl session
curl_close($ch);
// Return response body
return $responseBody;
}
}
You should note that I've commented out
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
You can put this option back in if you get an SSL certificate problem on your localhost, but you shouldn't use it in production .
You can then just do something like:
try{
$fitbitConnection = new FitbitConnection();
$token_response = $fitbitConnection->getToken("https://api.fitbit.com/oauth2/token","22942C","client_secret","1234567890","http://www.example.com");
echo $token_response;
}catch(Exception $e){
// curl error
echo $e->getMessage();
}

PAYPAL - Invalid Test API Credentials

Hi,
I have setup a business account at Paypal, and it seems that I already have my Test API Credentials, since I can successfully retrieve them from my Sandbox accounts.
Now I am trying to make the "first call" to get an authorization token.
Here's my PHP code:
$ch = curl_init();
$clientId = "myid"; //not the actual one
$secret = "mypass"; //not the actual one
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, "Accept: application/json");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
$json = json_decode($result);
print_r($json);
curl_close($ch);
And here's the response:
stdClass Object ( [error] => invalid_client [error_description] => Invalid client credentials )
Does anybody have a clue?
Thanks
EDIT: This login is working (I found it elsewhere):
clientId: ASF6RRBP0uTq7FnC90tpFx7vfA-Pliw8uQDjv5RZ10Y_NVspuc88pUPLN6yM
Secret: EAdx7BDKzWczDomYG2QDHu8jhaAXj4xDZLHadvL5aRfesjwo5c81zbSpRxuE
Strangely the format looks very different from mine.
I'm stuck here, cannot go any further without a token.
You could pass it as a header similar as follows:
"Authorization" => "Basic " . base64_encode($clientId . ":" . $clientSecret)
This should act the same as above.
You could write it on curl_setopt like this:
curl_setopt($ch, CURLOPT_HEADER, "Authorization: Basic " . base64_encode($clientId . ":" . $clientSecret));
Also, just out of curiosity, you might want to try out PayPal-PHP-SDK. They have all these bells and whistles, that would keep you away from manually getting this code. It has documentations, and samples, that would help you get started with any API fairly quickly.
finally i managed to get it to work
the correct credentials are a hash of 60-digit, not 16 as first seen
i had to create an app to get them
nobody told me that at paypal, not even the "merchant technical support"
hope this helps someone in the future
thanks for your answers

Categories