Jump to content

Recommended Posts

Posted (edited)

Hello,

I'm trying to implement the Facebook SDK in my Processwire website, but that is not going quite well...

I have an _init.php wherein I've put:

// FACEBOOK
require_once $config->paths->root.'facebook-sdk-v5/autoload.php';

$fb = new Facebook\Facebook([
  'app_id' => 'the_id',
  'app_secret' => 'the_secret',
  'default_graph_version' => 'v2.5'
]);

But that gives me the error (not finding the class):
Fatal error: Class 'ProcessWire\Facebook\Facebook' not found in /Users/iovalduc/Projectweb Media/Projecten/tits-and-party/Web/www/site/templates/_init.php on line 38

What am I doing wrong?
Is the root of the website a good place to put the SDK folder?
Should I check all the paths in the Facebook SDK scripts?
Will "__DIR__" in the SDK scripts ever work within Processwire?

Edited by KarlvonKarton
id and secret
Posted

You're using the ProcessWire namespace in that file, therefore you need to declare files explicitly as not part of your current namespace:

<?php
namespace ProcessWire;
// …
$fb = new \Facebook\Facebook([
  'app_id' => 'the_id',
  'app_secret' => 'the_secret',
  'default_graph_version' => 'v2.5'
]);

As you seem to use pw 3.0 you could also use composer instead of manually including the package.

  • Like 2
Posted

But, not there yet...

Fatal error: Exception: Cross-site request forgery validation failed. The "state" param from the URL and session do not match. (in /Users/iovalduc/Projectweb Media/Projecten/tits-and-party/Web/www/facebook-sdk-v5/Helpers/FacebookRedirectLoginHelper.php line 285) #0 /Users/iovalduc/Projectweb Media/Projecten/tits-and-party/Web/www/facebook-sdk-v5/Helpers/FacebookRedirectLoginHelper.php(249): Facebook\Helpers\FacebookRedirectLoginHelper->validateCsrf() #1 /Users/iovalduc/Projectweb Media/Projecten/tits-and-party/Web/www/site/templates/home.php(43): Facebook\Helpers\FacebookRedirectLoginHelper->getAccessToken() #2 /Users/iovalduc/Projectweb Media/Projecten/tits-and-party/Web/www/wire/core/TemplateFile.php(219): require('/Users/iovalduc...') #3 [internal function]: ProcessWire\TemplateFile->___render() #4 /Users/iovalduc/Projectweb Media/Projecten/tits-and-party/Web/www/wire/core/Wire.php(347): call_user_func_array(Array, Array) #5 /Users/iovalduc/Projectweb Media/Projecten/tits-and-party/Web/www/wire/core/WireHooks.php(548) in /Users/iovalduc/Projectweb Media/Projecten/tits-and-party/Web/www/index.php on line 64

Posted

The callback is sending a ?code=***  which gives the error (in the post above).

$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$loginUrl = $helper->getLoginUrl('http://{your-website}/login-callback.php', $permissions);

echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';

Any suggestions?

Posted

SOLVED:

I've put the getLoginUrl after the getAccessToken section and now the log in works.

 

<?php namespace ProcessWire;

// This file is the _init.php

$homepage = $pages->get('/'); 

// FACEBOOK
require_once $config->paths->root.'facebook-sdk-v5/autoload.php';

$fb = new \Facebook\Facebook([ // Thank you LostKobrakai 4 the \
  'app_id' => 'my_app_id',
  'app_secret' => 'my_app_secret',
  'default_graph_version' => 'v2.5'
]);

$helper = $fb->getRedirectLoginHelper();

try {
  $fbAccessToken = $helper->getAccessToken();
} catch(\Facebook\Exceptions\FacebookResponseException $e) { // Thank you LostKobrakai 4 the \
  // When Graph returns an error
  echo 'Graph returned an error: ' . $e->getMessage();
  exit;
} catch(\Facebook\Exceptions\FacebookSDKException $e) { // Thank you LostKobrakai 4 the \
  // When validation fails or other local issues
  echo 'Facebook SDK returned an error: ' . $e->getMessage();
  exit;
}

$loginUrl = $helper->getLoginUrl($homepage->httpUrl);

// Include shared functions
include_once("./_func.php"); 

Source: http://stackoverflow.com/questions/31347341/the-state-param-from-the-url-and-session-do-not-match

  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...