Last active
October 5, 2016 12:13
-
-
Save gunnrryy/c2c828fc2a77124cc1bed57af5e216df to your computer and use it in GitHub Desktop.
FB post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Class FacebookController () | |
{ | |
public function actionPost() { | |
$req = Yii::$app->controller->action->id; | |
$message = Yii::$app->request->post('message'); | |
// store the message in $_SESSION | |
$this->authenticate($req); | |
} | |
private function authenticate($req) { | |
$fb_cred = Yii::$app->params['fb_credential']; | |
$fb = new Facebook\Facebook([ | |
'app_id' => $fb_cred['app_id'], // Replace {app-id} with your app id | |
'app_secret' => $fb_cred['app_secret'], | |
'default_graph_version' => 'v2.2', | |
]); | |
$helper = $fb->getRedirectLoginHelper(); | |
$permissions = ['email', 'user_about_me', 'user_birthday', 'user_friends', 'read_custom_friendlists', 'publish_actions', 'user_posts']; // Optional permissions | |
$callBackUrl = Url::to(['facebook/callback?type=' . $req], true); | |
//path-to-Facebook-post-script-that-you-want-to-call-back-to | |
$loginUrl = $helper->getLoginUrl($callBackUrl, $permissions); | |
// header("Location:$loginUrl"); | |
//exit(); | |
return $this->redirect($loginUrl); | |
} | |
public function actionCallback() { | |
$fb_cred = Yii::$app->params['fb_credential']; | |
$fb = new Facebook\Facebook([ | |
'app_id' => $fb_cred['app_id'], // Replace {app-id} with your app id | |
'app_secret' => $fb_cred['app_secret'], | |
'default_graph_version' => 'v2.2', | |
]); | |
$helper = $fb->getRedirectLoginHelper(); | |
try { | |
$accessToken = $helper->getAccessToken(); | |
} catch (Facebook\Exceptions\FacebookResponseException $e) { | |
// When Graph returns an error | |
echo 'Graph returned an error: ' . $e->getMessage(); | |
exit; | |
} catch (Facebook\Exceptions\FacebookSDKException $e) { | |
// When validation fails or other local issues | |
echo 'Facebook SDK returned an error: ' . $e->getMessage(); | |
exit; | |
} | |
$oAuth2Client = $fb->getOAuth2Client(); | |
$tokenMetadata = $oAuth2Client->debugToken($accessToken); | |
$tokenMetadata->validateAppId($fb_cred['app_id']); // Replace {app-id} with your app id | |
$tokenMetadata->validateExpiration(); | |
if (!$accessToken->isLongLived()) { | |
try { | |
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken); | |
} catch (Facebook\Exceptions\FacebookSDKException $e) { | |
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n"; | |
exit; | |
} | |
} | |
Yii::$app->session->set('fb_access_token', $accessToken); | |
$req = Yii::$app->request->get('type'); | |
if (!empty($req)) { | |
if ($req == 'post') { | |
//retrive message to be posted from session that we set earlier | |
$message = Yii::$app->session->get('message'); | |
$this->post($fb, $accessToken, $message); | |
} | |
} | |
} | |
private function post($fb, $accessToken, $message) { | |
$params = ['message' => $message]; | |
try { | |
$response = $fb->post('/me/feed', $params, $accessToken); | |
if ($response->getHttpStatusCode() == '200') { | |
$successString = 'Successfully posted on your timeline'; | |
} else { | |
$errorString = 'Failed to post on your timeline'; | |
} | |
} catch (Facebook\Exceptions\FacebookResponseException $e) { | |
$errorString = 'Graph returned an error: ' . $e->getMessage(); | |
} catch (Facebook\Exceptions\FacebookSDKException $e) { | |
$errorString = 'Facebook SDK returned an error: ' . $e->getMessage(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment