I'm trying to post on my Facebook page with PHP.
I have set up the Facebook API (version 4) and collected all the data I need, like AppId, Secret Key, Access Token and now when I try to post on my page like this:
require 'vendor/autoload.php';
define('FACEBOOK_SDK_V4_SRC_DIR', '/vendor/facebook/php-sdk-v4/src/Facebook/');
define("FACEBOOK_CONSUMER_KEY", "{myKey}");
define("FACEBOOK_CONSUMER_SECRET", "{mySecret}");
define("FACEBOOK_ACCESS_KEY", "{myToken}");
Facebook\FacebookSession::setDefaultApplication(FACEBOOK_CONSUMER_KEY, FACEBOOK_CONSUMER_SECRET);
$session = new Facebook\FacebookSession(FACEBOOK_ACCESS_KEY);
$message = array(
"message" => "It works!",
"link" => "http://www.google.de",
//"picture" => "url to picture",
//"name" => "Picture title",
//"description" => "Description",
);
$response = ( new Facebook\FacebookRequest( $session, 'POST', '/{myPageId}/feed', $message ) )->execute()->getGraphObject();
echo $response->getProperty('id');
Unfortunately I get the following error evertime I execute it:
PHP Fatal error: Uncaught exception 'Facebook\FacebookPermissionException' with message '(#200) The user hasn't authorized the application to perform this action' in {myPath}/FacebookRequestException.php:128
Stack trace:
#0 {myPath}/FacebookRequest.php(280): Facebook\FacebookRequestException::create('{"error":{"mess...', Object(stdClass), 403)
#1 {myPath}/test.php(21): Facebook\FacebookRequest->execute()
#2 {main}
thrown in {myPath}/FacebookRequestException.php on line 128
How can I setup the Facebook App that it work correctly?
You have to do the login flow to authorize your app first. https://developers.facebook.com/docs/php/howto/example_facebook_login/4.0.0
Related
I've followed this tutorial to make a media upload to the twitter API. The text upload works perfectly, but if I want to upload a media, I get the error:
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'You must supply a readable file' in /hp/be/ac/on/www/vendor/abraham/twitteroauth/src/TwitterOAuth.php:297 Stack trace: #0 /hp/be/ac/on/www/vendor/abraham/twitteroauth/src/TwitterOAuth.php(266): Abraham\TwitterOAuth\TwitterOAuth->uploadMediaNotChunked('https://upload....', Array) #1 /hp/be/ac/on/www/posten.php(288): Abraham\TwitterOAuth\TwitterOAuth->upload('https://upload....', Array) #2 {main} thrown in /hp/be/ac/on/www/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 297
This is my code:
$twitter = new TwitterOAuth(
$config['consumer_key'],
$config['consumer_secret'],
$OAuthToken,
$OAuthTokenSecret
);
$post_media = $twitter->upload('media/upload', ['media' => "/uploads/Bilder/1.png"]);
/**$parameters = [
'status' => "Test description",
'media_ids' => implode(',', [$post_media->media_id_string])
];
$result = $twitter->post('statuses/update', $parameters);**/
Like you see, I've even commented the part, beginning at $parameters, but that didn't change the result. So the error must be in the upload line.
I've already tried replacing media/upload with https://upload.twitter.com/1.1/media/upload.json as said in https://github.com/abraham/twitteroauth/issues/562, but that didn't help either.
I've checked, that the link /uploads/Bilder/1.png is correct and refers to an image on my server. So, why doesn't the script work?
Does anybody know a solution to this?
It's late to answer, but might help someone. The code below works just perfect!
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$twitter = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token, $access_token_secret);
$content = $twitter->get("account/verify_credentials");
$tweet = "My tweet";
$imageMedia = $twitter->upload('media/upload', array('media' => '/path/to/image/image_name.png'));
$parameters = array("status" => $tweet, "media_ids" => $imageMedia->media_id_string);
$statuses = $twitter->post("statuses/update", $parameters);
Same problem, different solution.
Answer: "The upload() function requires a local file. An URL will not work, you must download it first."
Look at this issue: https://github.com/abraham/twitteroauth/issues/697
If you want to post image using twitter api at first you must download it first. In fact the upload() function requires a local file.
I am at the stage of the OAuth flow where I get the OAuth verifier and have to use it to get the permanent token, however I encounter the error "Invalid oauth_verifier parameter".
// I get a valid oauth verifier
$oauth_verifier = filter_input(INPUT_GET, "oauth_verifier");
// I am able to run var_dump($connection) and the response seems valid
$connection = new TwitterOAuth(
$config["consumer_key"],
$config["consumer_secret"],
$_SESSION["oauth_token"],
$_SESSION["oauth_token_secret"]
);
// I believe this is where the problem lies, if I try var_dump($token) nothing shows but my original error message
$token = $connection->oauth(
"oauth/access_token", [
"oauth_verifier" => $oauth_verifier
]
);
$twitter = new TwitterOAuth(
$config["consumer_key"],
$config["consumer_secret"],
$token["oauth_token"],
$token["oauth_token_secret"]
);
The full error message is:
Fatal error: Uncaught Abraham\TwitterOAuth\TwitterOAuthException: Invalid request token. in C:\xampp\htdocs\twitteroauth-master\src\TwitterOAuth.php:158 Stack trace: #0 C:\xampp\htdocs\twitter_callback.php(34): Abraham\TwitterOAuth\TwitterOAuth->oauth('oauth/access_to...', Array) #1 {main} thrown in C:\xampp\htdocs\twitteroauth-master\src\TwitterOAuth.php on line 158
If you are running this on a local server then check your url_callback, its not be in form of google tiny url, it must be in the form of "http://localhost:80/file_name" OR "http://localhots:8080/file_name". If you still have this problem then write your throw statement in try catch block.
e.g.
try {
throw new TwitterOAuthException($result);
}
catch(Exception $e) {
}
If you are running on server in spite of always check your url_callback should not be in the form of google tiny url because new twitter oauth didn't verify your google tiny url.
Right now i am working on spotify api, When i hit url for first time it is giving me all playlist but when i refresh page again it is giving me exception error, here is my code,
require '../../../vendor/autoload.php';
$session = new SpotifyWebAPI\Session('******', '*******', 'http://localhost:8080/spotify/vendor/jwilsson/spotify-web-api-php/demo.php');
$api = new SpotifyWebAPI\SpotifyWebAPI();
if (isset($_GET['code'])) {
$session->requestAccessToken($_GET['code']);
$data = $api->setAccessToken($session->getAccessToken());
$artistData = $api->me();
$artistId = $artistData->id;
$playlists = $api->getUserPlaylists($artistId, array(
'limit' => 5
));
foreach ($playlists->items as $playlist) {
echo '' . $playlist->name . ' <br>';
}
} else {
$scopes = array(
'scope' => array(
'user-read-email',
'user-library-modify',
),
);
header('Location: ' . $session->getAuthorizeUrl($scopes));
}
Here is my exception error
Fatal error: Uncaught exception 'SpotifyWebAPI\SpotifyWebAPIException' with message 'Invalid authorization code' in D:\xampp\htdocs\spotify\vendor\jwilsson\spotify-web-api-php\src\Request.php:156 Stack trace: #0 D:\xampp\htdocs\spotify\vendor\jwilsson\spotify-web-api-php\src\Request.php(26): SpotifyWebAPI\Request->send('POST', 'https://account...', Array, Array) #1 D:\xampp\htdocs\spotify\vendor\jwilsson\spotify-web-api-php\src\Session.php(233): SpotifyWebAPI\Request->account('POST', '/api/token', Array) #2 D:\xampp\htdocs\spotify\vendor\jwilsson\spotify-web-api-php\demo.php(31): SpotifyWebAPI\Session->requestAccessToken('AQAaVZvpXUExKg2...') #3 {main} thrown in D:\xampp\htdocs\spotify\vendor\jwilsson\spotify-web-api-php\src\Request.php on line 156
Can anyone please tell me why it cause error ? If anyone have experiance with spotify
Creator of the library here.
The only thing I can think of which would yield that error message is if you try to request a access token twice with the same code. Try visiting the URL to your PHP file again but remove any ?code=... parts.
Fatal error: Uncaught SoapFault exception: [Client] Function ("AuthenticationHeader") is not a valid method for this service in D:\xampp\htdocs\soap\index_2.php:132 Stack trace: #0 D:\xampp\htdocs\soap\index_2.php(132): SoapClient->__call('AuthenticationH...', Array) #1 D:\xampp\htdocs\soap\index_2.php(132): SoapClient->AuthenticationHeader(Array) #2 {main} thrown in D:\xampp\htdocs\soap\index_2.php on line 132
There are must be only one request, you send three. You must create something like this:
1) Set header for authentication:
$auth = $auth = new SOAPAuth('USERNAME', 'PASSWORD');
$header = new SOAPHeader('urn:example.org/auth', 'AuthenticationHeader', $auth);
$client->__setSoapHeaders($header);
2) Create create request with message text and contacts.
$result = $client->SendSMSToContacts(array("MessageText" => "some text", "contactIDs" => array(123456789, 123456789));
P.S. For the debugging create client with:
$client = new SoapClient($url, array('trace' => 1,
'exceptions' => 1,));
and after sending request, look on request that was sended and resopnse on it:
var_dump("REQUEST=", $client->__getLastRequest());
var_dump("RESPONSE=", $client->__getLastResponse());
if you look at the error code you will see AuthenticationHeader is not a method! the error is very clear!
use this
or download soap ui. (google it!)
and you will see what method are available!
I am using the new version 2, sdk 4.0 php. I am trying to send a notification to a user with my app. I just realised the access token that I get via login (user access token) is different to the access token for my app. I have searched online before posting it on here, but most is for the old sdk and cannot get the right syntax to work with mine.
My code:
$notification = (new FacebookRequest(
$session,
'POST',
'/'.$id[0].'/notifications',
array (
'href' => "http://localhost/app/index.php",
'template' => 'You have been tagged, please set privacy preference',
)
))->execute()->getGraphObject()->asArray();
And my error:
Fatal error: Uncaught exception 'Facebook\FacebookAuthorizationException' with message '(#15) This method must be called with an app access_token.' in C:\xampp\htdocs\app\Facebook\FacebookRequestException.php:134 Stack trace: #0 C:\xampp\htdocs\app\Facebook\FacebookRequest.php(268): Facebook\FacebookRequestException::create('{"error":{"mess...', Object(stdClass), 400) #1 C:\xampp\htdocs\app\privacy.php(121): Facebook\FacebookRequest->execute() #2 {main} thrown in C:\xampp\htdocs\app\Facebook\FacebookRequestException.php on line 134
Can someone help me with getting the app access token?
You can get the app access_token as follows:
$appSession = FacebookSession::newAppSession();
Then you can do:
$notification = (new FacebookRequest(
$appSession,
'POST',
'/'.$id[0].'/notifications',
array (
'href' => "http://localhost/app/index.php",
'template' => 'You have been tagged, please set privacy preference',
)
))->execute()->getGraphObject()->asArray();