Cannot get OAuth access token using the Twitter API - php

I'm creating a web app that will use Twitter API's and OAuth so that my app can post to my users twitter accounts.
Here is where I'm at so far - I get to the twitter authorization page.
Authorize the app to be able to post to my twitter account, which sends me to my callback file.
Get the oath_token and oauth_verifier info.
That's where I'm stuck. I CAN NOT seem to get the access token. Here is my callback code:
include 'db.php';
include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'secret.php';
$Twitter = new EpiTwitter($consumerKey, $consumerSecret);
// user comes from twitter
$Twitter->setToken($_GET['oauth_token']);
$token = $Twitter->getAccessToken();
setcookie('oauth_token', $token->oauth_token);
setcookie('oauth_token_secret', $token->oauth_token_secret);
$Twitter->setToken($token->oauth_token, $token->oauth_token_secret);
I've been researching and testing for about 5 hours and still nothing. I have tried to echo $token and it just comes out empty.
Am I missing something big here? seems like an easy task..? Thank you so much for any help :))))

Try this:
$Twitter = new EpiTwitter($consumerKey, $consumerSecret);
// user comes from twitter
$Twitter->setToken($_GET['oauth_token']);
$oauth_verifier = $_GET['oauth_verifier'];
$token = $Twitter->getAccessToken(array('oauth_verifier' => $oauth_verifier));
$Twitter->setToken($token->oauth_token, $token->oauth_token_secret);
$creds = $Twitter->get('/account/verify_credentials.json');
Hope it helps.

Related

Twitter sign in oauth api keeps redirecting me to authorize application

I'm working with twitter oauth and am having issues with twitter demanding me to authorize the usage of the application even if i have already authorized the application. For example, i authorize the usage of the application, log out, and then go to the login for twitter it asks me again for authorization of the usage of the application. Note my question is in regards to keeping me to authorize the application not the authentication area i imagine once the authorization issue is fixed the authentication of twitter will work.
The flow should be:
1./ User authorizes with twitter.
2./ Twitter sends to callback
3./ Callback performs database interaction for saving user and authenticating user with application
4./ Callback redirects to homepage
5./ User logs out of application (all session data destroyed)
6./ User re-logs in with twitter, and twitter should not reauthorize but recognize the user and redirect to application which should authenticate somehow from twitter possibly matching oauth tokens from a database?
Not sure if this is applicable, but if i even go to twitter login just after doing the authorization, it still asks me to reauthorize
Here's my code:
twitter login code
use Abraham\TwitterOAuth\TwitterOAuth;
$connection = new TwitterOAuth(twitter_apikey,twitter_apisecret);
$request_token= $connection->oauth('oauth/request_token', array('oauth_callback' => "https://example.com/twittercallback"));
$_SESSION['oauth_token']=$request_token['oauth_token'];
$_SESSION['oauth_token_secret']=$request_token['oauth_token_secret'];
$url = $connection->url("oauth/authorize", array("oauth_token" => $request_token['oauth_token']));
header('Location: '. $url);
And here's my callback code:
use Abraham\TwitterOAuth\TwitterOAuth;
$oauth_access_token = $_SESSION['oauth_token'];
$oauth_access_token_secret = $_SESSION['oauth_verifier'];
$connection = new TwitterOAuth(twitter_apikey,twitter_apisecret,$oauth_access_token , $oauth_access_token_secret );
$access_token = $connection->oauth('oauth/access_token', array('oauth_verifier' => $_REQUEST['oauth_verifier'], 'oauth_token'=> $_GET['oauth_token']));
$connection = new TwitterOAuth(twitter_apikey,twitter_apisecret, $access_token['oauth_token'], $access_token['oauth_token_secret']);
$user_info = $connection->get('account/verify_credentials',['include_email'=>'true']);
$oauth_token = $access_token['oauth_token'];
$oauth_token_secret = $access_token['oauth_token_secret'];
print "<pre>".print_r($user_info,true)."</pre>";
You need to redirect the user to https://api.twitter.com/oauth/authenticate instead of https://api.twitter.com/oauth/authorize
https://developer.twitter.com/en/docs/basics/authentication/api-reference/authenticate.html
Change this line
$url = $connection->url("oauth/authorize", array("oauth_token" => $request_token['oauth_token']));
to
$url = $connection->url("oauth/authenticate", array("oauth_token" => $request_token['oauth_token']));

Facebook Pages API Post Without User Login

I have created a FB App to publish posts as page from another site.
Many of the users will be publishing on my page, so I'm trying to create a function without user login or session.
I'm now doing it via creating Page Access Token manually from Graph API Explorer and injecting it into the code.
If I could create the access token in the code without user login, it will be my solution.
Is it possible or anyone knows how to do it?
Any help is appreciated.
P.S: In case anyone wonder my code, it is below.
$config['appId'] = $appId;
$config['secret'] = $secret;
$fb = new \Facebook($config);
$message = array(
'access_token' => $page_access_token, //manually injected
);
$message['message'] = 'message';
$message['link'] = 'link';
$posturl = '/'.$page_id.'/feed';
$result = $fb->api($posturl,'POST',$message);
There is example code in the docs, this is how to post stuff with a specific Access Token:
$response = $fb->post('/{page-id}/feed', $data, '{access-token}');
Source: https://developers.facebook.com/docs/php/howto/example_post_links
Of course you can´t create a Page Token without user login, that would be weird. You can only create a Page Token with a User Token, and you can only get a User Token with login. Extended Page Tokens are valid forever, so you only need to create it once and just store it.
More information about Tokens and how to generate them:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/

How to get user Access Token and Access Secret with the Twitter API

I'm trying to code a couple of PHP pages for getting users tokens the Twitter API 1.1. I’m using the TwitterOAuth library https://twitteroauth.com/
First page: twitter-go.php
The user opens it and gets redirected to twitter.com for authorizing the app.
I'm guessing this is where the POST oauth/request_token and GET oauth/authorize functions are being used.
Second page: twitter-back.php
The user gets redirected there from twitter once he authorizes the app. It then displays the user Access Token and the user Access Secret (or store them into a database for later use).
I'm guessing this is where the POST oauth/access_token function is being used.
Is this the correct way of getting a user Secret Token and Access Token?
Alright, actually managed to figure it out myself. Here is my code for those who need it:
First page: twitter-go.php
The user opens it and gets redirected to twitter.com for authorizing the app.
<?php
//LOADING LIBRARY
require "twitteroauth/autoloader.php";
use Abraham\TwitterOAuth\TwitterOAuth;
//TWITTER APP KEYS
$consumer_key = 'yourkey';
$consumer_secret = 'yourkey';
//CONNECTION TO THE TWITTER APP TO ASK FOR A REQUEST TOKEN
$connection = new TwitterOAuth($consumer_key, $consumer_secret);
$request_token = $connection->oauth("oauth/request_token", array("oauth_callback" => "http://boulangerie-colas.fr/twitter/twitter-back.php"));
//callback is set to where the rest of the script is
//TAKING THE OAUTH TOKEN AND THE TOKEN SECRET AND PUTTING THEM IN COOKIES (NEEDED IN THE NEXT SCRIPT)
$oauth_token=$request_token['oauth_token'];
$token_secret=$request_token['oauth_token_secret'];
setcookie("token_secret", " ", time()-3600);
setcookie("token_secret", $token_secret, time()+60*10);
setcookie("oauth_token", " ", time()-3600);
setcookie("oauth_token", $oauth_token, time()+60*10);
//GETTING THE URL FOR ASKING TWITTER TO AUTHORIZE THE APP WITH THE OAUTH TOKEN
$url = $connection->url("oauth/authorize", array("oauth_token" => $oauth_token));
//REDIRECTING TO THE URL
header('Location: ' . $url);
?>
Second page: twitter-back.php
The user gets redirected there from twitter once he authorizes the app. It then displays the user Access Token and the user Access Secret.
<?php
/**
* users gets redirected here from twitter (if user allowed you app)
* you can specify this url in https://dev.twitter.com/ and in the previous script
*/
//LOADING LIBRARY
require "twitteroauth/autoloader.php";
use Abraham\TwitterOAuth\TwitterOAuth;
//TWITTER APP KEYS
$consumer_key = 'yourkey';
$consumer_secret = 'yourkey';
//GETTING ALL THE TOKEN NEEDED
$oauth_verifier = $_GET['oauth_verifier'];
$token_secret = $_COOKIE['token_secret'];
$oauth_token = $_COOKIE['oauth_token'];
//EXCHANGING THE TOKENS FOR OAUTH TOKEN AND TOKEN SECRET
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $token_secret);
$access_token = $connection->oauth("oauth/access_token", array("oauth_verifier" => $oauth_verifier));
$accessToken=$access_token['oauth_token'];
$secretToken=$access_token['oauth_token_secret'];
//DISPLAY THE TOKENS
echo "<b>Access Token : </b>".$accessToken."<br />";
echo "<b>Secret Token : </b>".$secretToken."<br />";
?>
Please remember that you need to be using the using the TwitterOAuth library https://twitteroauth.com/

Facebook PHP-SDK setAccessToken NOT WORKING

IDEA: Get event information without user interaction from various facebook pages (clubs,bars,etc..). However some pages have set that info not to be public.
WORKING PRINCIPLE: I have the latest facebook PHP-SDK on my site and an APP on facebook, so usually i call
$facebook = new Facebook(array(
'appId' => '387262991341732',
'secret' => '09014d999f6e34d80ca3e62e331834cc',
));
$events = $facebook->api("$page_url/events");
PROBLEM:
When a page is not public I have to use an access_token.
1) This is an APP access-token, which is not permitted to view the events.
$app_token = $facebook->getAccessToken();
So I figure I need to get user access_token and add more code:
if (!$user) {
$args['scope'] = 'offline_access';`<br>`
$loginUrl = $facebook->getLoginUrl($args);`<br>`
}
<?php if (!$user): ?>
Login with Facebook
<?php endif ?>`
I click the link by myself and use getAccessToken() to get the user access_token:
$user_token = $facebook->getAccessToken();// I store this in my database
I check this with:
file_get_contents('http://graph.facebook.com/privatepage/events?access_token=$user_token');
// Everything works, im very happy
2) Now I delete all cookies, etc and try to use $facebook->setAccessToken($user_token) and then $events = $facebook->api("$page_url/events"); and get nothing. I assure you one more time that AccessToken is valid.
After quite a long research im stuck with this.
???) *Any Ideas how use $facebook->api(...) and not file_get_contents?

How to get data from facebook Ads on the Graph API?

I am new to PHP and facebook API'S
I am trying to get data from my ads account on faceobook using PHP
I just want to know how much $ every ads spent
First of all - I did not understand if I must have facebook app in order to get data from my personal ad account ?
I assumed yes so I created one now..,
I am geting the acess token like this :
require_once("facebook.php");
$config = array();
$config['appId'] = 'myapp_id';
$config['secret'] = 'myappsecret';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
// Set a new app secret
$facebook->setApiSecret($config['secret']);
// If you do above, also set the app id
$facebook->setAppId( $config['appId']);
$access_token = $facebook->getAccessToken();
But when I am trying a get request like this:
https://graph.facebook.com/act_[my_account_number]/adgroups
I am getting a premisson exception that tell me
An access token is required to request his resource
But where do I put the access token ?
You need to add it at the end of the url as a url parameter:
"https://graph.facebook.com/act_[my_account_number]/adgroups" . "?access_token=" . $access_token
However, from the code I think you are getting an App Access Token, and you need a Page Access Token or a User Access Token to get the ads related to your Facebook Page.
Please refer to https://developers.facebook.com/docs/facebook-login/access-tokens/ for more information.

Categories