I try to get Dropbox access token using PHP,
https://www.dropbox.com/developers-v1/core/docs#request-token
I made 2 php files,
auth.php:
$Header = array(
"Authorization: OAuth oauth_version=\"1.0\", oauth_signature_method=\"PLAINTEXT\", oauth_consumer_key=\"XXX\", oauth_signature=\"XXX&\"\r\n"
);
$Options = array('http' =>
array(
'method' => 'POST',
'header' => $Header,
)
);
$Context = stream_context_create($Options);
$Result = file_get_contents("https://api.dropbox.com/1/oauth/request_token", false, $Context);
echo '<a target="_blank" href="https://www.dropbox.com/1/oauth/authorize?'.$Result.'&oauth_callback=http://localhost/dropbox/access_token.php">auth</a>';
auth.php file is working good! and redirect me to dropbox site to accept me app
but when redirect to access_token.php file, i can't get my access token!
I got error: failed to open stream: HTTP request failed! HTTP/1.1
access_token.php file:
$Options = array('http' =>
array(
'method' => 'POST',
)
);
$au = $_GET['oauth_token'];
$Context = stream_context_create($Options);
$Result = file_get_contents("https://api.dropboxapi.com/1/oauth/access_token?oauth_token=$au", false, $Context);
print_r($Result);
Check selinux setting for httpd_network_connect in Apache. It seems that file get content fails to open the stream.
Related
Is there an easy way to send out push notifications in PHP without using external libraries? Here is my current code. when executed the server gives a 400 UnauthorizedRegistration error. I do not care about how fast or efficient the process is, I am fine with sending one notification at a time.
My PHP code is:
<?php
ini_set("display_errors","1");
$endpoint = "https://fcm.googleapis.com/fcm/send/fImmyvrGiSo:APA91bFg8A4N4GLmJMhouxPfw_R3ocv44uBaNkZu_JMVjAvueISJUJTQROpVxlBnX8PIeJGwE5s1pvLAldtcW-z6WzZ0Ixus2fc3jUADGbJ2L8kAdMv56S5cSmKMsFmPwDsdd2MoCrHC";
$postdata = http_build_query(
array(
"p256dh" => "BAHBGTIPl7M1v8Ui7EaNVlauLmi_jEJOreCQ3YTRu_nWgF4k8nwP5rewGWO86-3wP8yZ86GoKjHQpK_0sls05FI=",
"auth" => "V08681gA2Gs_2x_UNFCN0g=="
)
);
$opts = array('http' =>
array(
'ignore_errors' => true,
'method' => 'POST',
'header' => 'Content-type: application/json',
'content' => $postdata
)
);
$context = stream_context_create($opts);
// here file_get_contents is used to send a POST request
$result = file_get_contents($endpoint, false, $context);
echo $result;
?>
My push subscription is:
{"endpoint":"https://fcm.googleapis.com/fcm/send/dcgCC59wHCE:APA91bHEzFoal6MXgXWt16aSdGHFAeS7D4vre99pCvzU_QWK22YFkmYdUQ5OmiLmUUbuhyiFEDHg61TrpllZ-TcaSgqXXOIcyTUf_0I-ngZ--aH2OFnEIrB2eI-ZRiArHD5LVAAz1EiM","expirationTime":null,"keys":{"p256dh":"BAHBGTIPl7M1v8Ui7EaNVlauLmi_jEJOreCQ3YTRu_nWgF4k8nwP5rewGWO86-3wP8yZ86GoKjHQpK_0sls05FI=","auth":"V08681gA2Gs_2x_UNFCN0g=="}}
I have what I think is correctly written code yet whenever I try and call it I'm getting permission denied from Google.
file_get_contents(https://www.googleapis.com/urlshortener/v1/url): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden
This isn't a rate limit or anything as I currently have zero ever used...
I would have thought this is due to an incorrect API key but I've tried resetting it a number of times. There isn't some downtime while the API is first applied is there?
Or am I missing a header setting or something else just as small?
public function getShortUrl()
{
$longUrl = "http://example.com/";
$apiKey = "MY REAL KEY IS HERE";
$opts = array(
'http' =>
array(
'method' => 'POST',
'header' => "Content-type: application/json",
'content' => json_encode(array(
'longUrl' => $longUrl,
'key' => $apiKey
))
)
);
$context = stream_context_create($opts);
$result = file_get_contents("https://www.googleapis.com/urlshortener/v1/url", false, $context);
//decode the returned JSON object
return json_decode($result, true);
}
It seems I need to manually specify the key in the URL
$result = file_get_contents("https://www.googleapis.com/urlshortener/v1/url?key=" . $apiKey, false, $context);
This now works. There must be something funny with how the API inspects POST for the key (or lack of doing so).
Edit: For anyone in the future this is my complete function
public static function getShortUrl($link = "http://example.com")
{
define("API_BASE_URL", "https://www.googleapis.com/urlshortener/v1/url?");
define("API_KEY", "PUT YOUR KEY HERE");
// Used for file_get_contents
$fileOpts = array(
'key' => API_KEY,
'fields' => 'id' // We want ONLY the short URL
);
// Used for stream_context_create
$streamOpts = array(
'http' =>
array(
'method' => 'POST',
'header' => [
"Content-type: application/json",
],
'content' => json_encode(array(
'longUrl' => $link,
))
)
);
$context = stream_context_create($streamOpts);
$result = file_get_contents(API_BASE_URL . http_build_query($fileOpts), false, $context);
return json_decode($result, false)->id;
}
I have a simple file_get_contents using basic http auth:
$opts = array(
'http' => array(
'timeout' => 15,
'header' => array("Authorization: Basic "
. base64_encode("$user:$pwd"))
)
);
$context = stream_context_create($opts);
$data = file_get_contents($url, false, $context);
When I run this inside one PHP box, it arrives without the Authorization header. When I run it in another box, the header arrives.
What kind of info do I need to look for in order to find the cause?
I want to update my twitter status using the Twitter API. Here is my code:
$access_token = "{Twitter Access Token}";
$access_secret = "{Twitter Access Token Secret}";
$send_url = "https://api.twitter.com/1.1/statuses/update.json";
$opts = array('http' => array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded&oauth_token=$access_token&oauth_token_secret=$access_secret",
'content' => 'status=Hello'
) );
$context = stream_context_create($opts);
$result = file_get_contents($send_url, false, $context);
echo $result;
What am I doing wrong? Am I sending through the oAuth tokens correctly?
Any help would be much appreciated.
I have web app and api and my login system works with api so it retruns: header status 200 if it success(username and password are correctly) otherwise it returns HTTP/1.0 401 Unauthorized and json message e.x: {success: 0 , "wrong username/password"} (tested with POSTMEN plugin in google chrome)
For e.x if i want to make a request:
$method = "login";
$data = array("user"=>"test", "pass"=>"test");
send_re($method, $data);
this is my function send_re
function send_re($method, $data){
$url = "api.location/".$api_method;
$options = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query($data),
'header'=> "Content-Type: application/x-www-form-urlencoded\r\n"
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
return stripslashes( json_encode( $result) );
}
if my $data are correctly e.x username and password but if they are not I am not getting the error message but this:
A PHP Error was encountered
Severity: Warning
Message: file_get_contents(http://85.10.229.108/home/login): failed to open stream: HTTP request
HTTP/1.0 401 Unauthorized
Filename: libraries/api.php
Is there any way to escape this problem and get the message from api?
function send_re($method, $data){
$url = "api.location/".$api_method;
$options = array(
'http' => array(
'method' => 'POST',
'content' => http_build_query($data),
'header'=> "Content-Type: application/x-www-form-urlencoded\r\n",
'ignore_errors' => true
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
return stripslashes( json_encode( $result) );
}
Just add 'ignore_errors' => true and it's do the trick.
reference: http://php.net/manual/en/context.http.php