reddit api 403 forbidden - php

I have the following code;
It all works and I am able to obtain an access token but I get a 403 Forbidden Error when trying to use the submit endpoint. I saw a previous post that uses identical code pretty much, that mentioned that the endpoint is incorrect, Original Post.
I can't seem to find the updated endpoint; Is anyone familiar with the reddit api and would be able to help find the problem here. Thanks :-)
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$clientID = '****'; // app client id
$clientSecret = '****'; // app secret
$redditUsername = '****'; // reddit username
$redditPassword = '****'; // reddit password
// Post to subreddit info
$title = '****';
$r = '****';
$kind = '****';
$url = 'http://example.com';
$ch = curl_init('https://www.reddit.com/api/v1/access_token?grant_type=client_credentials&username=' . $redditUsername . '&password=' . $redditPassword . '');
curl_setopt($ch, CURLOPT_USERPWD, '' . $clientID . ':' . $clientSecret . '');
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close($ch);
$token = json_decode($return, true);
print_r($token);
$ch = curl_init('https://www.reddit.com/api/v1/submit?kind=link&sr=' . $r . '&title=' . $title . '&r=' . $r . '&url='.urlencode($url));
curl_setopt($ch, CURLOPT_USERPWD, '' . $clientID . ':' . $clientSecret . '');
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: bearer " . $token['access_token'] . ""));
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close($ch);
$response = json_decode($return, true);
echo '<br><br>';
print_r($response);
?>

Related

interswitch quickteller API integration in php

i have to add interswitch payment methods in my web application but i am receiving error
following is my code
function billersCategories()
{
$nonce=$randomNum=substr(str_shuffle("0123456789abcdefghijklmnopqrstvwxyz"), 0, 60);
$date = new DateTime();
$timestamp=$date->getTimestamp();
// Signature
$httpMethod = "GET";
$url='https://sandbox.interswitchng.com/api/v2/quickteller/categorys';
$clientId = "IKIA9D98ABCDEFGHIFAKEID1E09104959B9755C41E1";
$clientSecretKey = "d5uAr+U8QhSv8vQtKPDIUI62327Fsfsfsf65=";
$signatureCipher = $httpMethod."&".$url."&".$timestamp."&".$nonce."&".$clientId."&".$clientSecretKey;
$signature = base64_encode($signatureCipher);
$data = array("TerminalID" => "9APY556261");
$data_string = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Authorization:InterswitchAuth SUtJQTUyNTBERkY1NkU5MzM2OUM0RkRBRjMxQTQ3QTg1RkNDODYyRTRDOUU=',
'Signature:'.$signature,
'Nonce:'.$nonce,
'Timestamp:'.$timestamp,
'SignatureMethod:SHA512'
));
$result = curl_exec($ch);
echo curl_getinfo($ch) . '<br/>';
echo curl_errno($ch) . '<br/>';
echo curl_error($ch) . '<br/>';
var_dump($result);
}
But i am receiving following error
"The HTTP method is not supported for this resource", i tried http method POST but same error, i am new on API can someone please help me to solve this .
Use this instead, fill the following
In the variable
- $clientId
- $clientSecretKey
At the header
- TerminalID
<?php
$nonce=$randomNum=substr(str_shuffle("0123456789abcdefghijklmnopqrstvwxyz"), 0, 60);
$date = new DateTime();
$timestamp=$date->getTimestamp();
$httpMethod = "GET";
$clientId = "YOUR_OWN_ID";
$clientSecretKey = "YOUR_OWN_CLIENT_SECRET_KEY";
$resourceUrl='https://sandbox.interswitchng.com/api/v2/quickteller/categorys';
$resourceUrl = strtolower($resourceUrl);
$resourceUrl = str_replace('http://', 'https://', $resourceUrl);
$encodedUrl = urlencode($resourceUrl);
$transactionParams = "1";
$httpMethod = "GET";
$signatureCipher = $httpMethod . '&' . $encodedUrl . '&' . $timestamp . '&' . $nonce . '&' . $clientId . '&' . $clientSecretKey;
if (!empty($transactionParams) && is_array($transactionParams)) {
$parameters = implode("&", $transactionParams);
$signatureCipher = $signatureCipher . $parameters;
}
$signature = base64_encode(sha1($signatureCipher, true));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$resourceUrl);
// curl_setopt($ch, CURLOPT_POST, 1);
// curl_setopt($ch, CURLOPT_POSTFIELDS,$vars); //Post Fields
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = [
'Content-Type:application/json',
'Authorization:InterswitchAuth SUtJQTAzREM3RDY5NUREMzZFQURFNTQxNEE2Nzg1MUJCMUZFQ0Y5MUIxRjg=',
'Signature:'.$signature,
'Nonce:'.$nonce,
'Timestamp:'.$timestamp,
'SignatureMethod:SHA1',
'TerminalID:YOUR_ASSIGNED_TERMINAL_ID'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$server_output = curl_exec ($ch);
curl_close ($ch);
echo $server_output;
?>
This worked for me, hope it help someone in future

How to set username and password in PHP script for Salesforce login using Rest API

I'm trying to login to my Salesforce Account and retrieve some data. So far i have been able to login to Salesforce by providing the username and the password on the Salesforce login page and then redirecting to the data page which i have some echo statements. I want to know how to set the username and password in PHP itself, and make a successful Salesforce login.I used this documentation provided by Salesforce, Interact with the Force.com REST API from PHP
Please help.
Following is my config.php
<?php
ini_set('display_errors',1);
error_reporting(E_ALL ^ E_WARNING);
define("CLIENT_ID", "xxx");
define("CLIENT_SECRET", "xxx");
define("REDIRECT_URI", "https://xxxx/oauth_callback.php");
define("LOGIN_URI", "https://login.salesforce.com");
define("USERNAME","xxxx#xxx.com");
define("PASSWORD","*******");
define("SECURITY_TOKEN","xxxx");
?>
Following is my oauth.php
<?php
require_once 'config.php';
$auth_url = LOGIN_URI
. "/services/oauth2/authorize?response_type=code&client_id="
. CLIENT_ID . "&redirect_uri=" . urlencode(REDIRECT_URI);
header('Location: ' . $auth_url);
?>
Following is my oauth_callback.php
<?php
require_once 'config.php';
session_start();
$token_url = LOGIN_URI . "/services/oauth2/token";
$code = $_GET['code'];
if (!isset($code) || $code == "") {
die("Error - code parameter missing from request!");
}
$params = "code=" . $code
. "&grant_type=authorization_code"
. "&client_id=" . CLIENT_ID
. "&client_secret=" . CLIENT_SECRET
. "&username=" . USERNAME
. "&password=" . PASSWORD
. "&redirect_uri=" . urlencode(REDIRECT_URI);
$curl = curl_init($token_url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 200 ) {
die("Error: call to token URL $token_url failed with status $status, response $json_response curl_error " . curl_error($curl) . " curl_errno " . curl_errno($curl));
}
curl_close($curl);
$response = json_decode($json_response, true);
$access_token = $response['access_token'];
$instance_url = $response['instance_url'];
if (!isset($access_token) || $access_token == "") {
die("Error - access token missing from response!");
}
if (!isset($instance_url) || $instance_url == "") {
die("Error - instance URL missing from response!");
}
$_SESSION['access_token'] = $access_token;
$_SESSION['instance_url'] = $instance_url;
header( 'Location: demo_rest.php' ) ;
?>
Following is my demo_rest.php (shows the output)
$query = "SELECT Id FROM test__c limit 3";
$url = "$instance_url/services/data/v20.0/query?q=" . urlencode($query);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Authorization: OAuth $access_token"));
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
$json_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($json_response, true);
$total_size = $response['totalSize'];
echo "$total_size record(s) returned<br/><br/>";
echo "<br/>";
print_r($json_response);
Problem is you can not get the values of variables $instance_id and $access_token on your demo_rest.php file.
Replace the code of your demo_rest.php file with the one below.
<?php
session_start();
$access_token = $_SESSION['access_token'];
$instance_url = $_SESSION['instance_url'];
$query = "SELECT Id FROM test__c limit 3";
$url = "$instance_url/services/data/v20.0/query?q=" . urlencode($query);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array("Authorization: OAuth $access_token"));
curl_setopt($curl, CURLOPT_CAINFO, dirname(__FILE__) . '/cacert.pem');
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
$json_response = curl_exec($curl);
curl_close($curl);
$response = json_decode($json_response, true);
$total_size = $response['totalSize'];
echo "$total_size record(s) returned<br/><br/>";
echo "<br/>";
print_r($json_response);
?>
If it doesn't work, just leave me a comment.

Need an example using PHP and CURL to post XML to USPS address verification web service

I've tried the following:
<?php
$user = 'myusername';
$xml_data = "<AddressValidateRequest USERID='$user'>" .
"<IncludeOptionalElements>true</IncludeOptionalElements>" .
"<ReturnCarrierRoute>true</ReturnCarrierRoute>" .
"<Address ID='0'>" .
"<FirmName />" .
"<Address1 />" .
"<Address2>205 bagwell ave</Address2>" .
"<City>nutter fort</City>" .
"<State>wv</State>" .
"<Zip5></Zip5>" .
"<Zip4></Zip4>" .
"</Address>" .
"</AddressValidateRequest>";
$url = "http://production.shippingspis.com/ShippingAPI.dll";
//setting the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Following line is compulsary to add as it is:
curl_setopt($ch, CURLOPT_POSTFIELDS,
"?API=Verify&XML=" . $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$output = curl_exec($ch);
curl_close($ch);
$array_data = json_decode(json_encode(simplexml_load_string($output)), true);
print_r('<pre>');
print_r($array_data);
print_r('</pre>');
echo PHP_EOL;
?>
I get no response nor any kind of an error. Can anyone lead me in the right direction?
I have tried to follow several examples that I found on the web but I can't seem to hit upon the solution.
My code is based on the example in this post:
Send XML data to webservice using php curl
Working Code follows
<?php
$user = 'myusername';
$xml_data = "<AddressValidateRequest USERID='$user'>" .
"<IncludeOptionalElements>true</IncludeOptionalElements>" .
"<ReturnCarrierRoute>true</ReturnCarrierRoute>" .
"<Address ID='0'>" .
"<FirmName />" .
"<Address1>$address1></Address1>" .
"<Address2>$address2</Address2>" .
"<City>$city</City>" .
"<State>$state</State>" .
"<Zip5></Zip5>" .
"<Zip4></Zip4>" .
"</Address>" .
"</AddressValidateRequest>";
$url = "http://production.shippingapis.com/ShippingAPI.dll?API=Verify";
//setting the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Following line is compulsary to add as it is:
curl_setopt($ch, CURLOPT_POSTFIELDS,
'XML=' . $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$output = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
$array_data = json_decode(json_encode(simplexml_load_string($output)), true);
print_r('<pre>');
print_r($array_data);
print_r('</pre>');
echo PHP_EOL;
?>
Here is the working code. Note the XML is passed in the call to curl_setopt. In addition to the typo in my original URL, I included '&XML=' in the $url variable, which did not work.
<?php
$user = 'myusername';
$xml_data = "<AddressValidateRequest USERID='$user'>" .
"<IncludeOptionalElements>true</IncludeOptionalElements>" .
"<ReturnCarrierRoute>true</ReturnCarrierRoute>" .
"<Address ID='0'>" .
"<FirmName />" .
"<Address1>$address1></Address1>" .
"<Address2>$address2</Address2>" .
"<City>$city</City>" .
"<State>$state</State>" .
"<Zip5></Zip5>" .
"<Zip4></Zip4>" .
"</Address>" .
"</AddressValidateRequest>";
$url = "http://production.shippingapis.com/ShippingAPI.dll?API=Verify";
//setting the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Following line is compulsary to add as it is:
curl_setopt($ch, CURLOPT_POSTFIELDS,
'XML=' . $xml_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$output = curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
$array_data = json_decode(json_encode(simplexml_load_string($output)), true);
print_r('<pre>');
print_r($array_data);
print_r('</pre>');
echo PHP_EOL;
?>

Gmail Account Authentication from php

I am trying to authenticate the google account using this tutorial. I am passing just my username and password but it returning false. I am new to it. Here is the function i am calling
function googleAuthenticate($username, $password, $source = 'your_google_app', $service = 'lh2') {
$session_token = $source . '_' . $service . '_auth_token';
// get an authorization token
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
$post_fields = "accountType=" . urlencode('GOOGLE')
. "&Email=" . urlencode($username)
. "&Passwd=" . urlencode($password)
. "&source=" . urlencode($source)
. "&service=" . urlencode($service);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging the request
curl_setopt($ch, CURLOPT_VERBOSE, true); // for debugging the request
var_dump(curl_getinfo($ch,CURLINFO_HEADER_OUT));
echo "<br>";
var_dump($post_fields);
echo "<br>";
$response = curl_exec($ch);
curl_close($ch);
var_dump($response);
exit();
if (strpos($response, '200 OK') === false) {
return false;
}
// find the auth code
preg_match("/(Auth=)([\w|-]+)/", $response, $matches);
if (!$matches[2]) {
return false;
}
$_SESSION[$session_token] = $matches[2];
return $matches[2];
}

Are facebook notifications from either vb.net or PHP?

Does anyone know if its possible to create a Facebook notification either via Vb.Net or PHP using the Facebook SDK version 4.
Ideally I'd like to have users on my website enter their Facebook login details which I'd then store their [facebook id] so I could send a weekly status notification via VB.Net to their Facebook account.
Edited
So far I get the scope ID from javascript and for testing copy it to PHP
<?php
$appId = '77585851581xxxx';
$appSecret = 'xxxx';
$userId = '1015273548332xxxx'; //Scope ID
$accesstoken = $appId . '|' . $appSecret;
$notificationText = 'The cake is a lie.';
//Get Access Token
$args = array('grant_type' => 'client_credentials',
'client_id' => $appId,
'client_secret' => $appSecret);
$ch = curl_init();
$url = 'https://graph.facebook.com/oauth/access_token';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
$returnValue = curl_exec($ch);
curl_close($ch);
echo $returnValue;
$accesstoken = $returnValue;
//Set Notification
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'facebook');
$url = 'https://graph.facebook.com/' . $userId . '/notifications' .
'?access_token=' . $accesstoken .
'&template=' . $notificationText .
'&href=';
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);
echo $curlResult;
?>
I now get the error:
Sorry, something went wrong. We're working on getting this fixed as soon as we can.
Tutorial for the PHP SDK 4.0: http://www.devils-heaven.com/facebook-php-sdk-4-0-tutorial/
Although i would suggest using simple CURL calls for App Notifications, you donĀ“t need the PHP SDK for that. Example code can be found here: http://blog.limesoda.com/2014/08/app-notifications-facebook-apps/
The article is in german, but the code is the relevant part:
$accessToken = $appId . '|' . $appSecret;
$notificationText = 'The cake is a lie.';
//init curl
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'facebook');
//send notification
$url = 'https://graph.facebook.com/' . $userId . '/notifications' .
'?access_token=' . $accesstoken .
'&template=' . $notificationText .
'&href=';
curl_setopt($ch, CURLOPT_URL, $url);
$curlResult = curl_exec($ch);

Categories