دنبال کننده ها

۱۳۹۶ شهریور ۱۲, یکشنبه

Conflict with Facebook and Google login in php

[ad_1]



I am trying to implement both facebook and google auth in my website.I created Facebook Login first using facebook php sdk 2.2 and its working perfectlyi can retrive the user data.Then i made the Google login using Google api and its also working perfectly alone on its on.But when i try to combine these two scripts together i cannot login into any.Both are showing some error.



1.When i try to login through facebook it shows
"Uncaught exception 'Google_AuthException' with message 'Error fetching OAuth2 access token, message: 'invalid_grant"http://stackoverflow.com/" in C:wamp64wwwedusrcauthGoogle_OAuth2.php "



2.When i try to login through google it shows "Facebook SDK returned an error: Cross-site request forgery validation failed. Required param "state" missing."
this is after me selecting a google account.



I am not very experienced with api's so if any one can help it will be very much appreciated.Thank You.



index.php



<?php
//Include GP config file && User class

include_once 'gpConfig.php';
include_once 'gpUser.php';

require_once 'fbConfig.php';
require_once 'fbUser.php';

// --------------------GOOGLE AUTH-------------------------
if(isset($_GET['code']))
$gClient->authenticate($_GET['code']);
$_SESSION['token'] = $gClient->getAccessToken();
header('Location: ' . filter_var($redirectURL, FILTER_SANITIZE_URL));


if (isset($_SESSION['token']))
$gClient->setAccessToken($_SESSION['token']);


if ($gClient->getAccessToken())
//Get user profile data from google
$gpUserProfile = $google_oauthV2->userinfo->get();

//Initialize User class
$user = new gpUser();

//Insert or update user data to the database
$gpUserData = array(
'oauth_provider'=> 'google',
'oauth_uid' => $gpUserProfile['id'],
'first_name' => $gpUserProfile['given_name'],
'last_name' => $gpUserProfile['family_name'],
'email' => $gpUserProfile['email'],
'locale' => $gpUserProfile['locale'],
'picture' => $gpUserProfile['picture'],
'link' => $gpUserProfile['link']
);
$userData = $user->checkUser($gpUserData);

//Storing user data into session
$_SESSION['userData'] = $userData;

//Render facebook profile data
if(!empty($userData))
// $output = '<h1>Google+ Profile Details </h1>';
$gimg = '<img src="http://stackoverflow.com/".$userData['picture']."http://stackoverflow.com/" width="300" height="220">';
// $output .= '<br/>Google ID : ' . $userData['oauth_uid'];
$name= '<br/>Name : ' . $userData['first_name']."http://stackoverflow.com/".$userData['last_name'];
// $output .= '<br/>Email : ' . $userData['email'];
// $output .= '<br/>Gender : ' . $userData['gender'];
// $output .= '<br/>Locale : ' . $userData['locale'];
// $output .= '<br/>Logged in with : Google';
// $output .= '<br/><a href="http://stackoverflow.com/".$userData['link']."http://stackoverflow.com/" target="_blank">Click to Visit Google+ Page</a>';
$logout = '<br/>Logout from <a href="gplogout.php">Google</a>';
else
$output = '<h3 style="color:red">Some problem occurred, please try again.</h3>';

else
$authUrl = $gClient->createAuthUrl();
$glogin = '<a href="http://stackoverflow.com/".filter_var($authUrl, FILTER_SANITIZE_URL)."http://stackoverflow.com/"><img src="images/glogin.png" alt="http://stackoverflow.com/"/></a>';


// -----------------GOOGLE AUTH END HERE------------------------


// -----------------FACEBOOK AUTH STARTS HERE------------------------
if(isset($accessToken))
if(isset($_SESSION['facebook_access_token']))
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
else
// Put short-lived access token in session
$_SESSION['facebook_access_token'] = (string) $accessToken;

// OAuth 2.0 client handler helps to manage access tokens
$oAuth2Client = $fb->getOAuth2Client();

// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;

// Set default access token to be used in script
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);


// Redirect the user back to the same page if url has "code" parameter in query string
if(isset($_GET['code']))
header('Location: ./');


// Getting user facebook profile info
try
$profileRequest = $fb->get('/me?fields=name,first_name,last_name,email,link,gender,locale,picture');
$fbUserProfile = $profileRequest->getGraphNode()->asArray();
catch(FacebookResponseException $e)
echo 'Graph returned an error: ' . $e->getMessage();
session_destroy();
// Redirect user back to app login page
header("Location: ./");
exit;
catch(FacebookSDKException $e)
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;


// Initialize User class
$user = new fbUser();

// Insert or update user data to the database
$fbUserData = array(
'oauth_provider'=> 'facebook',
'oauth_uid' => $fbUserProfile['id'],
'first_name' => $fbUserProfile['first_name'],
'last_name' => $fbUserProfile['last_name'],
'email' => $fbUserProfile['email'],
'gender' => $fbUserProfile['gender'],
'locale' => $fbUserProfile['locale'],
'picture' => $fbUserProfile['picture']['url'],
'link' => $fbUserProfile['link']
);
$userData = $user->checkUser($fbUserData);

// Put user data into session
$_SESSION['userData'] = $userData;

// Get logout url
$logoutURL = $helper->getLogoutUrl($accessToken, $redirectURL.'fblogout.php');

// Render facebook profile data
if(!empty($userData))
// $output = '<h1>Facebook Profile Details </h1>';
$img = '<img src="http://stackoverflow.com/".$userData['picture']."http://stackoverflow.com/">';
// $output .= '<br/>Facebook ID : ' . $userData['oauth_uid'];
$name = $userData['first_name']."http://stackoverflow.com/".$userData['last_name'];
// $output .= '<br/>Email : ' . $userData['email'];
// $output .= '<br/>Gender : ' . $userData['gender'];
// $output .= '<br/>Locale : ' . $userData['locale'];
// $output .= '<br/>Logged in with : Facebook';
// $output .= '<br/><a href="http://stackoverflow.com/".$userData['link']."http://stackoverflow.com/" target="_blank">Click to Visit Facebook Page</a>';
$logout = '<a href="http://stackoverflow.com/".$logoutURL."http://stackoverflow.com/">Facebook</a>';
else
$output = '<h3 style="color:red">Some problem occurred, please try again.</h3>';


else
// Get login url
$loginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);

// Render facebook login button
$flogin = '<a href="http://stackoverflow.com/".htmlspecialchars($loginURL)."http://stackoverflow.com/"><img src="images/fblogin-btn.png"></a>';

// -----------------FACEBOOK AUTH ENDS HERE--------------------------
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login with Google using PHP by CodexWorld</title>
<style type="text/css">
h1font-family:Arial, Helvetica, sans-serif;color:#999999;
</style>
</head>
<body>
<div>
<?php if(isset($name))

echo $name;
echo '<br>' . $logout;

else
echo $flogin .'<br>' . $glogin;
?>

</div>
</body>
</html>


fbconfig.php



<?php
if(!session_id())
session_start();


// Include the autoloader provided in the SDK
require_once __DIR__ . '/facebook-php-sdk/autoload.php';

// Include required libraries
use FacebookFacebook;
use FacebookExceptionsFacebookResponseException;
use FacebookExceptionsFacebookSDKException;

/*
* Configuration and setup Facebook SDK
*/
$appId = '*****'; //Facebook App ID
$appSecret = '*****'; //Facebook App Secret
$redirectURL = 'http://localhost/edu/'; //Callback URL
$fbPermissions = array('email'); //Optional permissions

$fb = new Facebook(array(
'app_id' => $appId,
'app_secret' => $appSecret,
'default_graph_version' => 'v2.2',
));

// Get redirect login helper
$helper = $fb->getRedirectLoginHelper();

// Try to get access token
try
if(isset($_SESSION['facebook_access_token']))
$accessToken = $_SESSION['facebook_access_token'];
else
$accessToken = $helper->getAccessToken();

catch(FacebookResponseException $e)
echo 'Graph returned an error: ' . $e->getMessage();
exit;
catch(FacebookSDKException $e)
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;


?>


gpconfig.php



<?php
session_start();

//Include Google client library
include_once 'src/Google_Client.php';
include_once 'src/contrib/Google_Oauth2Service.php';

/*
* Configuration and setup Google API
*/
$clientId = '725839853784-p8c8j1lktp7t48s8v95el7af0208u5pk.apps.googleusercontent.com'; //Google client ID
$clientSecret = 's8yjf1lHhqN2BFLdPRpo3oPb'; //Google client secret
$redirectURL = 'http://localhost/edu/'; //Callback URL

//Call Google API
$gClient = new Google_Client();
$gClient->setApplicationName('Art Of Learning');
$gClient->setClientId($clientId);
$gClient->setClientSecret($clientSecret);
$gClient->setRedirectUri($redirectURL);

$google_oauthV2 = new Google_Oauth2Service($gClient);
?>



[ad_2]

لینک منبع