I am trying to run a FQL for the first time. This is the code I have included in my index.php file:
<?php $query="SELECT message FROM status WHERE uid = 544337058";echo $query; ?>
<?php
include_once ('facebook.php');
$api_key = 'XXXXXXXXXXXXXXX';
$secret = 'XXXXXXXXXXXXXXXXX';
global $facebook;
$facebook = new Facebook($api_key, $secret);
$result = $facebook->api_client->fql_query($query);
?>
<?php echo $result?>
I have logged into facebook from a different tab and just trying to read my own status message. However I am getting the following error:
SELECT message FROM status WHERE uid = ######
Fatal error: Uncaught exception 'FacebookRestClientException' with message 'Requires
user session' in /users/home/aafhe7vh/web/public/facebookapi_php5_restlib.php:3065 Stack
trace: #0 /users/home/aafhe7vh/web/public/facebookapi_php5_restlib.php(1025):
FacebookRestClient->call_method('facebook.fql.qu...', Array) #1 /users/home/aafhe7vh
/web/public/status.php(46): FacebookRestClient->fql_query('SELECT message ...') #2 {main}
thrown in /users/home/aafhe7vh/web/public/facebookapi_php5_restlib.php on line 3065
How do I get and provide a user's session to this query.
Thanks.
Try putting require_login method after $facebook object eg it should be like:
$facebook = new Facebook($api_key, $secret);
$facebook = $facebook->require_login();
If it is facebook application which it seems to be, also put this line below require_login:
$facebook = $facebook->require_frame();
Related
I started with facebook business sdk for php. Was following this doc: https://developers.facebook.com/docs/business-sdk/getting-started/
installed without trouble, then tried testing as they instructed,
created src/test.php
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Campaign;
use FacebookAds\Object\Fields\CampaignFields;
$app_id = "{app-id}";
$app_secret = "{appsecret}";
$access_token = "{access-token}";
$account_id = "act_{{adaccount-id}}";
Api::init($app_id, $app_secret, $access_token);
$account = new AdAccount($account_id);
$cursor = $account->getCampaigns();
// Loop over objects
foreach ($cursor as $campaign) {
echo $campaign->{CampaignFields::NAME}.PHP_EOL;
}
filled in the required values. and ran the file. Getting this:
FacebookAds\CrashReporter : Enabled
FacebookAds\CrashReporter : Exception detected!
FacebookAds\CrashReporter : Successfully sent report
Fatal error: Uncaught FacebookAds\Http\Exception\AuthorizationException: Invalid OAuth access token. in C:\laragon\www\testPro\vendor\facebook\php-business-sdk\src\FacebookAds\Http\Exception\RequestException.php:174
Stack trace:
#0 C:\laragon\www\testPro\vendor\facebook\php-business-sdk\src\FacebookAds\Http\Client.php(215): FacebookAds\Http\Exception\RequestException::create(Object(FacebookAds\Http\Response))
#1 C:\laragon\www\testPro\vendor\facebook\php-business-sdk\src\FacebookAds\Http\Request.php(286): FacebookAds\Http\Client->sendRequest(Object(FacebookAds\Http\Request))
#2 C:\laragon\www\testPro\vendor\facebook\php-business-sdk\src\FacebookAds\Api.php(165): FacebookAds\Http\Request->execute()
#3 C:\laragon\www\testPro\vendor\facebook\php-business-sdk\src\FacebookAds\Api.php(214): FacebookAds\Api->executeRequest(Object(FacebookAds\Http\Request))
#4 C:\laragon\www\testPro\vendor\facebook\php-business-sdk\src\FacebookAds\ApiRequest.php(187): FacebookAds\Api->call('/act_3667840345...', 'GET', Array, Array)
#5 in C:\laragon\www\testPro\vendor\facebook\php-business-sdk\src\FacebookAds\Http\Exception\RequestException.php on line 174
Not sure what I am doing wrong. Didnt find much by searching. Can anyone please help?
The error you get is pretty explicit: "Invalid OAuth access token"
you can check the token you supplied using Facebook's token-debbuger # https://developers.facebook.com/tools/debug/accesstoken/
most probably you are missing the "ads_management" permission.
I created a subscription for my Android app and here's my code to confirm subscription assignments, but I get an error
$packageName = "com.example.srkn";
$productId = "com.example.srkn.aylik";
$token = "<some token here>";
require 'vendor/autoload.php';
$client = new \Google_Client();
$client->setAuthConfig('ts.json');
$client->addScope('https://www.googleapis.com/auth/androidpublisher');
$service = new \Google_Service_AndroidPublisher($client);
$postBody = "okey";
$para = "test";
$purchase = $service->purchases_subscriptions->acknowledge($packageName, $productId, $token,$postBody,$para);
//echo json_encode($purchase);
Fatal error: Uncaught TypeError: Argument 4 passed to
Google_Service_AndroidPublisher_Resource_PurchasesSubscriptions::acknowledge()
must be an instance of
Google_Service_AndroidPublisher_SubscriptionPurchasesAcknowledgeRequest,
string given, called in /home/serkanka/public_html/dgr/onayla.php on
line 17 and defined in
/home/serkanka/public_html/dgr/vendor/google/apiclient-services/src/Google/Service/AndroidPublisher/Resource/PurchasesSubscriptions.php:40
Stack trace: #0 /home/serkanka/public_html/dgr/onayla.php(17):
Google_Service_AndroidPublisher_Resource_PurchasesSubscriptions->acknowledge('com.text.spec.s...',
'com.text.spec.s...','jlkehndmfjoaenc...', 'test', 'test') #1 {main}
thrown in
/home/serkanka/public_html/dgr/vendor/google/apiclient-services/src/Google/Service/AndroidPublisher/Resource/PurchasesSubscriptions.php
on line 40
The postbody (4th parameter) should be an instance of Google_Service_AndroidPublisher_SubscriptionPurchasesAcknowledgeRequest
You can set the developerPayload on the object like so:
$requestBody = new Google_Service_AndroidPublisher_SubscriptionPurchasesAcknowledgeRequest();
$requestBody->setDeveloperPayload('your_developer_payload');
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.
I am developing a mobile application with googleplus login. where I get the access token and send it to my server in order to verify it using Google APIs Client Library for PHP.
When I try to authenticate the token I got a Fatal Error Saying :
Fatal error: Uncaught exception 'Google_Auth_Exception' with message 'Error fetching OAuth2 access token, message: 'invalid_client'' in C:\xxx\vendor\google\apiclient\src\Google\Auth\OAuth2.php:127 Stack trace: #0 C:\xxx\vendor\google\apiclient\src\Google\Client.php(130): Google_Auth_OAuth2->authenticate('ya29.2QBihTdAag...') #1 C:\xxx\index.php(20): Google_Client->authenticate('ya29.2QBihTdAag...') #2 {main} thrown in C:\xxx\vendor\google\apiclient\src\Google\Auth\OAuth2.php on line 127
and here is my PHP code:
<?php
$google_client_id = 'XXXXX.apps.googleusercontent.com';
$google_client_secret = 'XXXXX';
$google_redirect_url = 'xxx';
$google_developer_key = 'XXXXXXXX';
$google_application_name = 'XXXX Login';
$google_application_scope = 'email'; /* I only needed the basic user info */
$google_redirect_uri='';
//include google api files
require_once 'vendor/autoload.php';
$gClient = new Google_Client();
$gClient->setApplicationName($google_application_name);
$gClient->setClientId($google_client_id);
$gClient->setClientSecret($google_client_secret);
$gClient->setRedirectUri($google_redirect_uri);
$gClient->setScopes($google_application_scope);
$gClient->setDeveloperKey($google_developer_key);
$gClient->authenticate("ya29.2QBihTdAagwpH5hHJUg5soIETLspVUgle6VPOT52UYPWkOKyHdUJlpUyvoMKPgkuqIS4PvgXQhfpHw");//Error Happened here
$token = json_decode($gClient->getAccessToken());
$google_oauthV2 = new Google_Service_Oauth2($gClient);
$user_info = $google_oauthV2->userinfo->get();
var_dump(user_info);
die();
?>
For $google_client_secret, instead of 'notasecret' use the value in the "CLIENT SECRET" section of your "Client ID for web application".
I'm trying to implement a way for users to login with Google and with Facebook as part of a college assignment. I had my "Login with Google" code working nicely. I then started to try to implement my "Login with Facebook code".
<?php
//required files for Google+ login
require_once 'google-api-php-client/src/apiClient.php';
require_once 'google-api-php-client/src/contrib/apiPlusService.php';
//include this file containing specific app details (key, clientID, redirect etc) and get user details (image and name in this case)
include 'php/googleplus.php';
//required for facebook login
require 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxx',
'secret' => 'xxxx',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
?>
........
........
<?php if ($user):
$fblogoutUrl = $facebook->getLogoutUrl();
?>
Logout
<?php else:
$fbloginUrl = $facebook->getLoginUrl();
?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<div id="login">
<!--Load the php code that contains the user login options for google-->
<?php
include 'php/userlogin.php';
?>
I've not altered the code much at all as far as i can tell - only added my own app id, secret and redirect. When i click on the "Login with Facebook" link i get the following error message:
**Fatal error**: Uncaught exception 'apiAuthException' with message 'Error fetching OAuth2 access token, message: 'invalid_grant'' in /Applications/MAMP/htdocs/www/Shared sites/LOGIN ASSIGNMENT/google-api-php-client/src/auth/apiOAuth2.php:105 Stack trace: #0 /Applications/MAMP/htdocs/www/Shared sites/LOGIN ASSIGNMENT/google-api-php-client/src/apiClient.php(138): apiOAuth2->authenticate(Array) #1 /Applications/MAMP/htdocs/www/Shared sites/LOGIN ASSIGNMENT/php/googleplus.php(19): apiClient->authenticate() #2 /Applications/MAMP/htdocs/www/Shared sites/Login Assignment/index.php(7): include('/Applications/M...') #3 {main} thrown in **/Applications/MAMP/htdocs/www/Shared sites/LOGIN ASSIGNMENT/google-api-php-client/src/auth/apiOAuth2.php** on line **105**
It's clearly trying to access the Google php files for some reason. I'm not experienced enough to diagnose the problem. I'm wondering has anyone else encountered this error and how would i go about solving this problem, thanks.
You can solve this problem by specifying external logout problem. You can have a look at here
for detail information. It is a good tutorial for this problem.
Hope this helps
For Google login, you can use LightOpenID.Download it from here
include 'openid.php';
$openid = new LightOpenID();
$openid->identity = 'google.com/accounts/o8/id';
$openid->required = array('namePerson/first', 'namePerson/last', 'contact/email');
$openid->returnUrl = 'your_return_url';
$googleLoginUrl = $openid->authUrl();
thats it
you can check if user logged in or not like
if ($openid->mode) { //User logged in}