How to post a status update to Sina Weibo via OAuth2? - php

I'm trying to post a status update to the Chinese microblogging website Sina Weibo, via PHP/cURL and OAuth2.
I'm running into this error:
{"error":"auth
faild!","error_code":21301,"request":"/2/statuses/update.json"}
My PHP:
<?php
$ch = curl_init('https://api.weibo.com/2/statuses/update.json');
$headers = array(
'Authorization: Bearer '.$access_token,
'Content-Type: application/x-www-form-urlencoded');
$postData = array('access_token' => '2.00x123456789', 'status' => 'hello');
curl_setopt_array($ch, array(
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => TRUE,
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postData
));
$response = curl_exec($ch);
echo $response;
curl_close($ch);
?>
I authorized the app with the OAuth2 scope all and the token is valid.
What could be the reason for the error?

Remove your http header
'Content-Type: application/x-www-form-urlencoded'
Replace post fields with:
CURLOPT_POSTFIELDS => http_build_query($postData)
And now Be Happy!

Related

Api payment gateway integration PHP

My boss received a payment API ( it allows customer to pay in many instalments ) and asked me to integrate it to my website
But I'm a developper beginner and I don't really know where to start.
If I understood correctly, I need to get access token first.
The Authorization type is : OAuth 2.0
Header Prefix : Bearer
Grant Type : Client Credientials
Scope : Resource.WRITE, resource.READ
Authorization data to : Request Header
How can I get the access token and refresh it ?
I tried something like this : ( but it isn't working )
<?php
$clientId = 'myclientid';
$clientSecret = 'myclientsecret';
function getToken(){
$curl = curl_init();
$params = [
CURLOPT_URL => 'https://test.auth.alphacredit.be/api/oauth/token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_POST => 1,
CURLOPT_NOBODY => false,
CURLOPT_HTTPHEADER => array(
"content-type: application/json",
"accept: */*",
"accept-encoding: gzip, deflate",
'Authorization: Bearer',
),
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'client_id' => $clientId,
'client_secret' => $clientSecret,
'grant_type' => 'client_credentials',
'scope' => 'read, write',))
];
curl_setopt_array($curl, $params);
$response = curl_exec($curl);
curl_close($curl);
}

Postmates API using php/curl - returns unauthorized / Invalid OAuth 2.0 credentials provided

I got myself some postmates customer and api credentials (https://partner.postmates.com/welcome-api)
followed the example here https://documenter.getpostman.com/view/3967924/RW1Yq1oN#993c52be-abcc-b06b-3f3d-0abfe9782484 like so
$customerId = 'cus_xxxxxxxxxx';
$apiKey = 'aaaaaa-bbbbb-ccc-dddd-eeeeeeee';
$authKey = base64_encode($apiKey.':');
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.postmates.com/v1/customers/'.$customerId.'/delivery_quotes',
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('pickup_address' => '76 9th Ave, New York, NY','dropoff_address' => '46 Maujer St, Brooklyn, NY'),
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json',
'Authorization: Basic '.$authKey.'',
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
exit();
(of course using the actual customerId and apiKey)
but i only ever get as a result the following:
{"code":"unauthorized","message":"Invalid OAuth 2.0 credentials provided."}
(the CURLOPT_SSL_VERIFYPEER = false is just added for simplicities sake in a test environment here, using it on a live server with ssl verified yields the same results)
I have of course double and triplechecked the customerid and api key, but the result does not change
If anyone has any idea what i am missing , that would be great as i have been banging my head against the wall for hours now

Error on cURL request using php-cURL while sending SMS

The request entity's media type 'multipart/form-data' is not
supported for this resource.\",\"ExceptionMessage\"unsure emoticon"No
MediaTypeFormatter is available to read an object of type 'SmsQueue'
from content with media type
'multipart/form-data'.\",\"ExceptionType\"unsure
emoticon"System.Net.Http.UnsupportedMediaTypeException\",\"StackTrace\"unsure
emoticon" at
System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent
content, Type type, IEnumerable 1 formatters, IFormatterLogger
formatterLogger, CancellationToken cancellationToken)\r\n at
System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage
request, Type type, IEnumerable`1 formatters, IFormatterLogger
formatterLogger, CancellationToken cancellationToken)\"}"
// Prepare you post parameters
$postArray = array(
'APIKey' => AUTH_KEY,
'number' => $mobile,
'text' => $message,
'senderid' => SENDER_ID,
'channel' => $channel,
'DCS' => $DCS,
'flashsms' => $flashsms,
'route' => $route
);
// Init the resource
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postArray
));
// Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
// Get response
$curlOutput = curl_exec($ch);
// Print error if any
if (curl_errno($ch)) {
echo 'error:' . curl_error($ch);
}
curl_close($ch);
CURLOPT_POSTFIELDS => http_build_query($postArray) not working
curl_setopt($cURLConnection, CURLOPT_POSTFIELDS => http_build_query($postRequest1));
Set Content-Type in header to application/x-www-form-urlencoded.
Use like this :
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://url.com",
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => http_build_query(['message' => test]),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
));

Dropbox HTTP API - PHP cURL added header item boundary automatically

I'm new with the Dropbox API integrations, and I'm using the PHP cURL extension to make calls to the HTTP REST API, and when I try to make a request I receive the following string:
Error in call to API function "files/list_folder":
Bad HTTP "Content-Type" header:
"text/plain; boundary=----------------------------645eb1c4046b".
Expecting one of "application/json", "application/json; charset=utf-8",
"text/plain; charset=dropbox-cors-hack".
I'm sending this with code very similar to this:
$sUrl = "https://api.dropboxapi.com/2/files/list_folder";
$oCurl = curl_init($sUrl);
$aPostData = array('path' => '', 'recursive' => true, 'show_hidden' => true);
$sBearer = "MY_TOKEN";
$aRequestOptions = array(
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/plain',
'Authorization: Bearer ' . $sBearer),
CURLOPT_POSTFIELDS => $aPostData,
CURLOPT_RETURNTRANSFER => true);
curl_setopt_array($aRequestOptions);
$hExec = curl_exec($oCurl);
if ($hExec === false){
// Some error info in JSON format
} else {
var_dump($hExec);
}
As you have it, you're doing a multipart form upload, which isn't what the API expects.
There are a few things you need to do differently:
You should be sending up the parameters as JSON in the body.
You should set the Content-Type to application/json, accordingly.
There isn't a show_hidden parameter on /files/list_folder, but perhaps you meant to send include_deleted.
The curl_setopt_array method takes two parameters, the first of which should be the curl handle.
Here's an updated version of your code that works for me:
<?php
$sUrl = "https://api.dropboxapi.com/2/files/list_folder";
$oCurl = curl_init($sUrl);
$aPostData = array('path' => '', 'recursive' => true, 'include_deleted' => true);
$sBearer = "MY_TOKEN";
$aRequestOptions = array(
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array('Content-Type: application/json',
'Authorization: Bearer ' . $sBearer),
CURLOPT_POSTFIELDS => json_encode($aPostData),
CURLOPT_RETURNTRANSFER => true);
curl_setopt_array($oCurl, $aRequestOptions);
$hExec = curl_exec($oCurl);
if ($hExec === false){
// Some error info in JSON format
} else {
var_dump($hExec);
}
?>

How to connect to the new assembla key?

The new assembla api provides by REST-access a new authentification. i would like connect with PHP and curl, but I am not sure how I can include the api-x-key and api-x-secret as options:
The invoke with curl in terminal:
curl -H "X-Api-Key: XXX" -H "X-Api-Secret: XXX" https://api.assembla.com/v1/spaces/XXX/tickets.json
in PHP (my problem):
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => ' https://api.assembla.com/v1/spaces/XXX/tickets.json',
CURLOPT_POSTFIELDS => ???maybe???
));
$response = curl_exec($ch);
print_r($response);
This is my first try, without the options from api-key/api-secret including.
Send those keys as headers. Try this:
$headers = array('X-Api-Key: YOUR_KEY',
'X-Api-Secret: YOUR_SECRET'
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => ' https://api.assembla.com/v1/spaces/XXX/tickets.json',
CURLOPT_HTTPHEADER => $headers
));
$response = curl_exec($ch);
print_r($response);
Hope this helps.
Assembla API in PHP:
$headers = array('X-Api-Key: YOUR_KEY',
'X-Api-Secret: YOUR_SECRET'
);
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => ' https://api.assembla.com/v1/spaces/XXX/tickets.json',
CURLOPT_HTTPHEADER => $headers
));
$response = curl_exec($ch);
print_r($response);
This code prints a blank page.
No response
help required please

Categories