Generate Pinterest access token dynamically with PHP API - php

I developed a Pinterest plugin for WordPress users. Now, I am facing token generation problem with Pinterest API. I have set and configure accordingly to API docs. In Step 1 you get an access code and in Step 2, you need to request for access token with the help of access code. Step 1 going to succeed but in Step 2 throwing error response.
I already asked to Pinterest Support through email but they are saying:
We're a small team right now and we can't offer development support or
consult for the API just yet. For more support around using our API,
we encourage you to use developer resources like Stack Overflow.
The code I did:
I already made an APP and used the give App ID and Secret, and redirect URL also set in APP same below:
$client_id = "&client_id=496200555XXXXXXXX1778834";
$client_secret = "&client_secret=48d62d7c21aa432bb5320c0aeXXXXXXXXXXX75933f6295db1bae61ffa66ca31";
$authorization_url = "https://api.pinterest.com/oauth/?";
$response_type = "response_type=code";
$state = "&state=weblizar_app";
$scope = "&scope=read_public,read_relationships";
$redirect_uri = "&redirect_uri=https://weblizar.com/pinterest-access-token.php";
$access_token_url = "https://api.pinterest.com/v1/oauth/token?";
$grant_type = "grant_type=authorization_code";
// Step 1: get authorization code
$access_code_url = $authorization_url . $response_type . $redirect_uri . $client_id . $scope . $state;
echo "<a href=$access_code_url>Get Authorization Code</a>";
// Step 2: exchange the access token
if(isset($_GET['code'])) {
$authorization_code = $_GET['code'];
$access_code_url = $access_token_url . $grant_type . $client_id . $client_secret ."&code=" .$authorization_code; echo "<br>";
echo "Curl post URL - "; echo "<br>";
$ch = curl_init();
echo $url = $access_code_url;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = trim(curl_exec($ch));
curl_close($ch);
echo "Curl Post Response"; echo "<br>";
echo "<pre>";
print_r($result);
echo "<pre>";
}
Response Returned After Step 2
{
"message": "405: Method Not Allowed",
"type": "http"
}
As everyone knows Pinterest token generation URL no more available for users or closed down.
You can test, the code is live on site: https://weblizar.com/pinterest-access-token.php
Any kind of help really appreciated. Thanks in advance.

Related

Creating User on ownCloud using php curl http post

There is all the relevant information present in broken form in the following links on owncloud related websites and from stackoverflow itself:
User Provisioning Api - Owncloud
PHP + curl, HTTP POST sample code
Create user on ownCloud using Ajax Jquery
User Provisioning - php Authentication error
I am trying to do something very simple :
I have setup an owncloud server in my localhost,
I have an html page that takes in string values of user name and password
I send the page request to be processed by the following php script.
<?php
echo "Begun processing credentials , first it will be stored in local variables" . "<br/>";
// Loading into local variables
$userName = $_POST['username'];
$RRpassword = $_POST['password'];
echo "Hello " . $userName . "<br/>";
echo "Your password is " . $RRpassword . "<br/>";
// Add data, to owncloud post array and then Send the http request for creating a new user
$ownCloudPOSTArray = array('username' => $userName, 'password' => $RRpassword );
$url = 'http://localhost/owncloud/ocs/v1.php/cloud/users';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ownCloudPOSTArray);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo "<br/>Created a new user in owncloud";
?>
I get the output like:
Begun processing credentials , first it will be stored in local
variables Hello Frank Your password is frankspassword
failure 997 Unauthorised Created a new user in owncloud
I also tried to login to own cloud using following php script:
// Login As Admin
$ownAdminname = 'ownAdmin';
$ownAdminpassword = 'ownAdminPassword';
$ch = curl_init('http://localhost/owncloud');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$ownAdminname:$ownAdminpassword");
$output = curl_exec($ch);
curl_close($ch);
echo $output;
Even this one fails.
So in short it doesn't work. I am also unable to login via similar script to owncloud. What is the proper way to do this ? What settings am I missing ? Can someone help please ?
Since this question pertains to owncloud specifically, I created an account and posted a question linking this one to it in owncloud forum.
There I was suggested by the owncloud master #RealRancor, the following,
Just had another look, maybe its just easy to replace:
$url = 'http://localhost/owncloud/ocs/v1.php/cloud/users';
with
$url =
'http://adminuser:adminpass#localhost/owncloud/ocs/v1.php/cloud/users';
as shown in the documentation.
And amazingly it worked like a charm. So here is the entire modified php script:
<pre>
<?php
echo "Begun processing credentials , first it will be stored in local variables" . "<br/>";
// Loading into local variables
$userName = $_POST['username'];
$RRpassword = $_POST['password'];
echo "Hello " . $userName . "<br/>";
echo "Your password is " . $RRpassword . "<br/>";
// Login Credentials as Admin
$ownAdminname = 'ownAdmin';
$ownAdminpassword = 'ufi2016%%';
// Add data, to owncloud post array and then Send the http request for creating a new user
$url = 'http://' . $ownAdminname . ':' . $ownAdminpassword . '#localhost/owncloud/ocs/v1.php/cloud/users';
echo "Created URL is " . $url . "<br/>";
$ownCloudPOSTArray = array('userid' => $userName, 'password' => $RRpassword );
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ownCloudPOSTArray);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo "Response from curl :" . $response;
echo "<br/>Created a new user in owncloud<br/>";
// Add to a group called 'Users'
$groupUrl = $url . '/' . $userName . '/' . 'groups';
echo "Created groups URL is " . $groupUrl . "<br/>";
$ownCloudPOSTArrayGroup = array('groupid' => 'Users');
$ch = curl_init($groupUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ownCloudPOSTArrayGroup);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo "Response from curl :" . $response;
echo "<br/>Added the new user to default group in owncloud";
?>
</pre>
And here is the output:
Begun processing credentials , first it will be stored in local variables
Hello Frank
Your password is frankspassword
Created URL is http://ownAdmin:ufi2016%%#localhost/owncloud/ocs/v1.php/cloud/users
Response from curl : ok 100
Created a new user in owncloud
Created groups URL is http://ownAdmin:ufi2016%%#localhost/owncloud/ocs/v1.php/cloud/users/Frank/groups
Response from curl : ok 100
Added the new user to default group in owncloud
The owncloud documentation states that authentication is done by means of a basic HTTP Authentication header. What you are currently doing is sending the credentials as parameters to the API call. You need to add the following line:
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $RRpassword);
There's also a typo in CURLOPT_RETURNTRANSFER ($curl instead of $ch).
As your linked post (Create user on Owncloud) shows you need to an basic auth header with some credentials. You need to provide the admin credentials, afaict.

It is possible to get my current location via the Facebook Graph API?

I am trying to build a portion into my personal website that shows the locations I have tagged in my posts on facebook. My understanding is that I need to do an oAuth Request on the server to get the AccessToken:
UPDATE
I built a php file that gets a file with the token in it and refreshes it. I can call this file with a cron job every hour and have an unlimited token. It looks like this:
$appID = "APPID";
$appSecret = "APPSECRET";
$currentToken = file_get_contents(__DIR__."/token.txt");
$url = "https://graph.facebook.com/oauth/access_token?client_id=".$appID."&client_secret=".$appSecret."&grant_type=fb_exchange_token&fb_exchange_token=".$currentToken;
$ci = curl_init();
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ci, CURLOPT_TIMEOUT, 10);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ci, CURLOPT_FOLLOWLOCATION, TRUE);
$newtoken = curl_exec($ci);
curl_close ($ci);
$newtoken = str_replace("access_token=","",$newtoken);
$newtoken = substr($newtoken, 0,strpos($newtoken, "&expires=",0));
if($newtoken == "")
{
echo "error";
}
else
{
$file = __DIR__."/token.txt";
file_put_contents($file, $newtoken);
echo $newtoken;
}
If the Access Token has the form {app_id}|{app_secret}, you're not using a User Access Token, but an App Access Token.
Seems like you don't implement the proper Login process to gather the User Access Token. Stick to the provided sample code of the FB PHP SDK.
See
https://developers.facebook.com/docs/php/gettingstarted/4.0.0
https://developers.facebook.com/docs/facebook-login/access-tokens#usertokens

Google Drive OAuth => response=Moved Temporarily

I'm trying to write a small PHP code that should be able to authenticate itself to a Google Account and then upload a file to its Google Drive.
This process should be done in two steps:
send the authentication request and get in exchange the authorization code
POST the authorization code and get in exchange the access token that should be used to send other requests (eq: a file upload) to the Google Drive
Note that by using the Google Client Library everything works smoothly.
What I want to achieve is to NOT use the Google Client Library but instead to use the simple authentication steps described here.
By using these authentication steps mentioned earlier I send a request:
https://accounts.google.com/o/oauth2/auth?scope=email%20profile&redirect_uri=http://example.com&response_type=code&client_id=[my-client-id]
and in exchange the redirected page should contain the "code=xxxx" parameter, like:
http://example.com?code=xxx
where xxx is the authorization code prepended by Google to the redirect_uri mentioned above.
The problem is that the Google returns an error message like Moved Temporarily and also provides a link "The document has moved here.". If I click that link then everything works just fine.
But I don't want the user intervention because this code works at the server level and not at the client level.
How to solve this? Please don't answer with "use the Google Client Library!" because I've already said that by doing so it will work but I don't want the Google Client Library dependency!
My PHP code is:
$CLIENT_ID = '[the client ID from Google Developer Console]';
$CLIENT_SECRET = '[the client secret from Google Developer Console]';
$REDIRECT_URI = 'http' . ($_SERVER['SERVER_PORT'] == 80 ? '' : 's') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
$ch = curl_init();
if (isset($_GET['code']))
{
$url = 'https://accounts.google.com/o/oauth2/token';
$post_fields = 'code=' . $_GET['code'] . '&client_id=' . $CLIENT_ID . '&client_secret=' . $CLIENT_SECRET . '&redirect_uri='
. $REDIRECT_URI . '&grant_type=authorization_code';
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('Host:accounts.google.com', 'Content-Type:application/x-www-form-urlencoded', 'Content-Length:' . strlen($post_fields))
);
}
else
{
$url = sprintf('https://accounts.google.com/o/oauth2/auth?scope=email%%20profile&redirect_uri=%s&response_type=code&client_id=%s',
$REDIRECT_URI, $CLIENT_ID
);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
$error_no = curl_errno($ch);
curl_close($ch);
echo $result;
?>
The solution for this kind of problem is:
don't call the first request via CURL but by changing the document Location
Also change this block:
...
else
{
$url = sprintf('https://accounts.google.com/o/oauth2/auth?scope=email%%20profile&redirect_uri=%s&response_type=code&client_id=%s',$REDIRECT_URI, $CLIENT_ID);
}
with this one:
...
else
{
$url = sprintf('https://accounts.google.com/o/oauth2/auth?scope=email%%20profile&redirect_uri=%s&response_type=code&client_id=%s',$REDIRECT_URI, $CLIENT_ID);
header('Location:'.$url);
}

google oauth2 set scope ? cant get email

Hi im useing googles api oauth2 for a login system and mostly i've got what i want,,, all except the email, after trying all day i have to ask for help. here is my code
<?php
$client_key = 'xxxxxxxx49-6gplrhtl5xxxxxxx5gerurei0o.apps.googleusercontent.com';
$client_secret = 'WLMQxxxxxxxxxxxxxxofyjv11';
$api_key = '9xxxxxxxx0749';
$scope = 'https://www.googleapis.com/auth/userinfo#email';
if(!isset($_REQUEST['code'])){
echo "no Good";
} else {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "code=".$_REQUEST['code']."&client_id=".$client_key."&client_secret=".$client_secret."&redirect_uri=https://localhost/admin/index.php&grant_type=authorization_code");
$data = curl_exec($ch);
curl_close();
$data = json_decode($data);
//var_dump($data);
$access_token = $data->access_token;
echo $access_token;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/plus/v1/people/me?access_token=".$access_token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
curl_close();
$data = json_decode($data);
echo $data->displayName;
//HERE IS WHERE IM HAVING DRAMA
echo $data->emails->values
echo "<img src=\"".$data->image->url."\"><br>";
echo '<pre>';
var_dump($data);
echo '</pre><br>';
}
?>
I must be close but .. im stumped
I also tried adding this
$scope = array('https://www.googleapis.com/auth/userinfo.email','https://www.googleapis.com/plus/v1/people/me','https://www.googleapis.com/auth/userinfo.profile');
But i had no luck what so ever
Your scope string is wrong there at the top. The easiest one to use is just... "email" ! (as in the word, no quotes).
Make sure all your scopes are valid before signing people in - it looks like you want to use scopes email and profile. These shortened version replace https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email. The longer scopes will still work, but the others you have aren't looked at scopes at all - no scopes have # in them, and all URL form scopes are under googleapis.com/auth.
One other problem - the email will be in an emails array - so it will be $data->emails[0]->value you are looking for (and its worth checking that the type field is "account" too, in case more than one is returned).
You can try it on the API explorer: https://developers.google.com/+/api/latest/people/get - try selecting the Oauth 2.0 toggle, remove all scopes in there and put email and profile as two custom scopes.

PHP get users latest twitter feed using twitter Oauth tools (consumer key , access token , consumer secret , access secret)

I need to get the twitter latest feed of the user. i used
https://api.twitter.com/1/statuses/user_timeline.json?oauth_token=ecwmW3pMB8euREHVljdySRexys6c6XYangqEANY&screen_name=twitter_name&count=1
but i get "Rate limit exceeded" cliente could not make 150 request per hour.
I used twitter Oauth request using consumer key, consumer secret , access token and access secret and the code as follows
$oauth_consumer_key="Vqlj7vd9yHo5MZPnWGf3w";
$oauth_nonce="af00a3a26b15ec0178ce0342acc9b392";
$oauth_signature_method="HMAC-SHA1";
$oauth_timestamp="1341408794";
$oauth_token="620811780-30jD4XHWFvp15RcjmxHkLLicFlvMDuNaWp6fKjia";
$oauth_version="1.0";
$oauth_signature=rawurldecode("N6L4VJLJjY%2BPKxoelttqe2GOPAw%3D");
$url = "https://api.twitter.com/1/statuses/user_timeline.json?";
$url .= "screen_name=twitter_name";
$url .= "&oauth_consumer_key=".$oauth_consumer_key."";
$url .= "&oauth_token=".$oauth_token."";
$url .= "&oauth_nonce=".$oauth_nonce."";
$url .= "&oauth_signature_method=".$oauth_signature_method."";
$url .= "&oauth_timestamp=".$oauth_timestamp."";
$url .= "&oauth_version=1.0";
$url .= "&oauth_signature=".$oauth_signature."";
$url .= "&count=1";
$url .= "&include_rts=true";
$url .= "include_entities=true";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: api.twitter.com'));
$json = curl_exec($ch);
curl_close ($ch);
$data = json_decode($json, true);
print_r($json);
still i get the rate limit exceeded error.
could any one help me get twitter latest feed using Oauth.
Thanks.
From experience Twitter OAuth is hit and miss if you try to handle negotiations yourself. I tried for several weeks to achieve this but kept having errors like 'Rate limit exceeded' and 'Invalid OAuth Key'. I checked out a library and within the same hour I could get statuses, set statuses, send tweets with and without pics etc..
Here's what I'm using now, test it out. It's lightweight and easy to use.
https://github.com/themattharris/tmhOAuth/

Categories