SammyK / LaravelFacebookSdk

Fully unit tested Facebook SDK v5 integration for Laravel & Lumen
MIT License
693 stars 201 forks source link

You must provide an access token. #179

Open johnef opened 7 years ago

johnef commented 7 years ago

This is my controller

<?php

namespace App\Http\Controllers;

use App\Models\VoteCat;
use Illuminate\Http\Request;
use SammyK;
use Facebook;
use App\Http\Requests;

class VoteUserController extends Controller
{
    public function index(SammyK\LaravelFacebookSdk\LaravelFacebookSdk $fb)
    {
        try {
            $token = $fb->getAccessTokenFromRedirect();
        } catch (Facebook\Exceptions\FacebookSDKException $e) {
            // Failed to obtain access token
            dd($e->getMessage());
        }

        // $token will be null if the user denied the request
        if (! $token) {
            // User denied the request
        }
        // Send an array of permissions to request
        $login_url = $fb->getLoginUrl(['email']);

        $response = $fb->get($login_url);
        $eventNode = $response->getGraphEvent();

        $user = $response->getGraphUser();
        echo 'Name: ' . $user['email'];
    }
}

Got

You must provide an access token.

How should I provide this access token.

apasov commented 7 years ago

Try this:

// $token will be null if the user denied the request
if (! $token) {
    // User denied the request
} else {
    // set valid $token as default access token
    $fb->setDefaultAccessToken($token);
}