PHP Cronjob Why doesn't this OAuth request POST my twitter status - php

I have a quite specific question here.
I have been working with Cronjobs to get my Twittermessages automatically POSTed. I found the OAuth library by Abraham Williams.
This is what I did so far on my setup:
require_once ('Abraham/TwitterOAuth/autoloader.php');
use Abraham\TwitterOAuth\TwitterOAuth;
$consumerKey = "XXXXX"; //add the key from your app
$consumerSecret = "XXXXX"; //add the secret from your app
$accessToken = "XXXXX"; //add the access token from your app
$accessSecret = "XXXXX"; //add the access secret from your app
$message = "This is another Test";
$connection = new TwitterOAuth($consumerKey,$consumerSecret,$accessToken,$accessSecret);
$connection->post("statuses/update", array("status" => $message));
My question is here. If I load this into my browser, shouldn't this already automatically send a Tweet?
I have setup my own Twitter app and I also set all permissions to read and write.

Related

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/

Twitter does not connect with localhost PHP

<?
// here we want to send the article to twitter
require_once('twitteroauth.php');
// Twitter Connection Data
$tConsumerKey       = 'xxxx';
$tConsumerSecret    = 'xxxx';
$tAccessToken       = 'xxxx';
$tAccessTokenSecret = 'xxxx';
// start connection
$tweet = new TwitterOAuth($tConsumerKey, $tConsumerSecret, $tAccessToken, $tAccessTokenSecret);
// the message
$message = 'Auto Tweet via PHP ...';
// send to twitter
$tweet->post('statuses/update', array('status' => $message));
?>
here is my code .. it does not send post tweet. is it about localhost or something else ?
I'm using xampp on windows7 ..
Which library are you using for OAuth? Is it for an app that you have made or somebody elses?
If you can edit and check the settings on the Twitter developer area, this question about using localhost with your twitter application might be useful for your callback when authenticating your app.
Twitter oAuth callbackUrl - localhost development
I found it useful when wanting to connect apps during development. Using 127.0.0.1.

facebook graph api user returns 0

I am trying to use Facebook integration with Graph API
I registered with FaceBook, got my appID and Secret, downloaded the facebook-php-sdk-master and used this code:
require_once("facebook-php-sdk-master/src/facebook.php");
$config = array();
$config[‘appId’] = 'YOUR_APP_ID';
$config[‘secret’] = 'YOUR_APP_SECRET';
$config[‘fileUpload’] = false; // optional
$facebook = new Facebook($config);
$user = $facebook->getUser();
echo "user = ".$user;
Now when I try to use this code it always prints user = 0.
Have i Missed anything ?
ps: I have authorized the app too.
PPS: Could this because I am trying to test my code from localhost and facebook needs a website name for this to run ?
Thanks
You need to make sure that you have authorized the application. Just because you created the app does not mean the app knows who you are or can access your data.
Here is more info about logging in: https://developers.facebook.com/docs/technical-guides/login/
You first need to login and authorize the app.

Error in Foursquare API

I'm currently working with the foursquare API. I downloaded the files from github right here https://github.com/jmathai/foursquare-async. But, when I put my credentials in like my clientId, my client secret, and my redirectUri, it doesn't quite work; it says that there's a redirect uri mismatch. The beginning of the code in the simpleTest.php file looks like this:
ob_start();
require_once 'EpiCurl.php';
require_once 'EpiFoursquare.php';
$clientId = 'CLIENT_ID';
$clientSecret = 'CLIENT_SECRET';
$code = 'CODE';
$accessToken = 'ACCESS_TOKEN';
$redirectUri = 'http://www.thered-line.com/foursquare/simpleTest.php';
$userId = '4855602';
$fsObj = new EpiFoursquare($clientId, $clientSecret, $accessToken);
$fsObjUnAuth = new EpiFoursquare($clientId, $clientSecret);
How to get my $code and $accessToken... ?
This library is for using Foursquare with oAuth. That means that you get your code and access token from part of the oAuth handshake. Foursquare provided you with the client information - the rest is done in the oauth handshake.
When you changed the URL and the user, but kept the code and access token from the original test, you ended up with a code and token that were invalid - you are using the tokens from a handshake that does not have the same data anymore. If you change the test back to how it was on Github, it should run.
Basically, all you need for this lib is the clientID and the Secret - the rest will be done with PHP function calls from the library.
More info

OAuth authorization without Twitter login page

I use http://github.com/abraham/twitteroauth PHP library for reaching the Twitter REST API. My config file has credentials:
<?php
$consumer_key = 'xxxxxxxxxxxxx';
$consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$login = 'xxx';
$passwd = 'xxx';
?>
Is it possible to get oauth_token and oauth_token_secret without showing the Twitter login page?
As the comments state, you can't do this. Once the you have authorised, you can then store the returns and use them in all your calls, keeping your connection "persistant"
go to http://dev.twitter.com/apps and click on your app
then, on the right side click on "My Access Tokens"

Categories