Facebook SDK-PHP : getting the uid of the connected user - php

I'm trying to get the UID of the connected user with Facebook SDK for PHP, but it doesn't work, and all other things work properly; it means that the problem is not in the connection to the application.
I tried this code but it is always returning 0 while I'm connected to Facebook; I even tried with other accounts but no solution.
$uid = $facebook->getUser();

$facebook = facebook_get();
$uid = $facebook->getUser();
echo $uid;
and here's facebook_get()
function facebook_get()
{
include('lib/fbapi/facebook.php');
$config = array();
$config['appId'] = '...';
$config['secret'] = '...';
$facebook = new Facebook($config);
return $facebook;
}

Can you post your authentication code you've coded or you're using?
If you post, we can look into your code and give you the answer to why it's returning 0, it could be your authentication code which is not coded properly or it could be config error.etc.

Related

Dropbox PHP SDK not working on production server

I am playing around with Dropbox PHP SDK and I have a wired problem. On my localhost environment it works like a charm, but on my server it does not. I have ssl and the dropbox redirect urls is: https://flaviuscojocariu.com/arhitecta/dropbox_finished.php
I don't understand where the problem can be, as I searched my entire code for http://localhost/ links and replaced everything. I get only the 500 error and I don't understand why.
Can someone advice?
If needed I can provide more code. This is the code which I use for auth.
<?php
session_start();
$_SESSION['user_id'] = 1;
require __DIR__.'/../vendor/autoload.php';
$dropboxKey = '***';
$dropboxSecret = '***';
$appName = '***';
$appInfo = new Dropbox\AppInfo($dropboxKey,$dropboxSecret);
// Store CSRF token
$csrfToken = new Dropbox\ArrayEntryStore($_SESSION, 'dropbox-auth-csrf-token');
// Define auth details
$webAuth = new Dropbox\WebAuth($appInfo, $appName, 'http://localhost/arhitecta/dropbox_finished.php', $csrfToken);
$db = new PDO('mysql:host=127.0.0.1;dbname=arhitecta;', 'root','');
//User datails
$user = $db->prepare("
SELECT * FROM users WHERE id = :user_id
");
$user->execute(['user_id' => $_SESSION['user_id']]);
$user = $user->fetchObject();

Twitter oAuth login. How to get getAuthorizeURL(authorize) for first time and (authenticate) once user has granted permissions

I am using Twitter oAuth PHP Library (by Abraham Williams) for login-using-twitter for my web-application.
A.The Code model looks like this:
1.The Callback URL in twitter settings is set to :
http://example.com/entrypoint/twitterlogin.php
2.The twitterlogin.php file
<?PHP
if (empty($_GET['oauth_verifier'])){
$request_token = $twitteroauth->getRequestToken();
$_SESSION['onetime_oauth_token'] = $request_token['oauth_token'];
$_SESSION['onetime_oauth_token_secret'] = $request_token['oauth_token_secret'];
}
if (!empty($_GET['oauth_verifier']) && !empty($_SESSION['onetime_oauth_token']) && !empty($_SESSION['onetime_oauth_token_secret'])) {
$twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, $_SESSION['onetime_oauth_token'], $_SESSION['onetime_oauth_token_secret']);
$access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);
$GoesToDB = $access_token['oauth_token'];
$GoesToDBS = $access_token['oauth_token_secret'];
$user_info = $twitteroauth->get('account/verify_credentials');
$GoesToDBArray = $user_info;
}else{
if ($twitteroauth->http_code == 200) {
//generates ~/authenticate?oauth_token=token
$url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);
//generates ~/authorize?oauth_token=token
//$url = $twitteroauth->getAuthorizeURL($request_token['oauth_token'], false);//when and how to use this
exit(header("refresh:0;url=".$url));
} else {
//some error handler
}
}
?>
B.Expected behavior/results
If the user is first time user then generate authorize url (~/authorize?oauth_token=token)
Once the user has granted the permissions to the application generate authenticate url (~/authenticate?oauth_token=token) for future logins
C.Queries
Is the approach correct for the expected behavior
Do i have ask the user to use authorize url if he is an first time user, else use the authenticate url(similar the signin and signup)
Is there way to check if the user has authorized the application using (onetime_oauth_token or something else)
D.Thanking you in advance :) .. you are a life saviour.
Use this -
$url = $twitteroauth->getAuthorizeURL($request_token['oauth_token'],TRUE);

Facebook PHP Sdk Access Token

I know there are tons of topics
on this question, but every single one that I've seen, seems to have no solution at all!
I'm getting the infamous "Oauth Exception - An active access token must be used to query information about the current user" and I don't know what to do.
My facebook connect was working perfectly fine until yesterday, now, it stopped working all of a sudden today.
Can anyone please help me with this?
I'm using php sdk to connect the user to facebook in a pop-up window, then I make him autorize my app and redirect the window to a php called facebookReturn.php.
In this file, I'm supposed to get the user info, save it on Session, then close the window and refresh the main page behind.
Here are my codes.
index.php:
require_once 'phps/facebook.php';
$config = array();
$config[appId] = 'xxxx';
$config[secret] = 'xxxx';
$config[fileUpload] = false;
$facebook = new Facebook ($config);
$facebookId = $facebook->getUser();
echo "<br>facebook id: $facebookId";
if ($facebookId) {
$userProfile = $facebook->api('/me');
$logoutUrl = $facebook->getLogoutUrl();
}
else {
$params = array ();
$params[scope] = 'publish_stream, user_birthday, email, user_activities';
$params[redirect_uri] = 'http://www.mydomain.com.br/phps/facebookReturn.php';
$params[display] = 'popup';
$loginUrl = $facebook->getLoginUrl($params);
}
facebookReturn.php:
require_once 'facebook.php';
$config = array();
$config[appId] = 'xxxx';
$config[secret] = 'xxxx';
$config[fileUpload] = false;
$facebook = new Facebook ($config);
$user = $facebook->getUser();
$userProfile = $facebook->api('/me');
Thank you in advance.
Remomber: Facebook token expires in 60 days.

$me = $facebook->api('/me'); returns empty string

This piece of code used to work as expected, but now it seems i can't retrieve the user name the same way:
$uid = $facebook->getUser(); // returns the facebook id of the user
$me = $facebook->api('/me'); // returns empty. why?
Due some facebook api updates i had to change the getSession from:
$session = $facebook->getUser();
to
$session = $facebook->getUser()>0;
So how to retrieve the user name in facebook?
So how to retrieve the user name in facebook?
What about this :
$pageContent = file_get_contents('http://graph.facebook.com/USERID');
$parsedJson = json_decode($pageContent);
echo $parsedJson->name;
Facebook use CURL for send request to server, maybe curl on your server not work or curl_exec is disabled

php zend gdata - Get list of google docs using oauth

I've got my session with the valid token that i set up this way :
$session_token = Zend_Gdata_AuthSub::getAuthSubSessionToken($_GET['token']);
// Store the session token in our session.
$_SESSION['cal_token'] = $session_token;
Then i want to be able to do this:
$service = Zend_Gdata_Docs::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, $service);
$docs = new Zend_Gdata_Docs($client);
$feed = $docs->getDocumentListFeed();
But using the token. Instead of the authentication with user/pass/service
I already looked at some example of this but i didn't find any way to make it work.
Thank you everyone!
// Retrieve user's list of Google Docs
$client = Zend_Gdata_AuthSub::getHttpClient($_SESSION['cal_token']);
$docs = new Zend_Gdata_Docs($client);
$feed = $docs->getDocumentListFeed();
foreach ($feed->entries as $entry) {
echo "$entry->title\n";
}

Categories