php oauth league2 composer package

Viewed 18

I am working with the github oauth2 authentication system, and i am using League/oauth2-client composer package for php. I have gotten the token but i now want to make a request to the github endpoint to get the user details. Now github uses bearer token to make a request to and endpoint. How can i come up with that using this package. I am using laravel. Heres the code:

   <?php

namespace App\Http\Controllers\usercontroller;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;
use League\OAuth2\Client\Provider\GenericProvider;

use function PHPUnit\Framework\returnSelf;

class githubcontroller extends Controller
{
    private $gitprovider;
    // private $accesstoken;
    public function __construct()
    {
        $this->gitprovider = new GenericProvider([
            'clientId'                => env('GITCLIENTID'),    // The client ID assigned to you by the provider
            'clientSecret'            => env('GITCLIENTSECRET'),    // The client password assigned to you by the provider
            'redirectUri'             => env('GITredirectUri'),
            'urlAuthorize'            => env('GITurlAuthorize'),
            'urlAccessToken'          => env('GITurlAccessToken'),
            'urlResourceOwnerDetails' => env('GITurlResourceOwnerDetails')
        ]);
        // $this->accesstoken = Session::get('gitaccesstoken');
    }
    public function home(){
        $accesstoken = Session::get('gitaccesstoken');

$context = stream_context_create(array(
    'http' => array(
        'header' => "Authorization: Bearer " . $accesstoken,
    ),
));

// $result = file_get_contents($api_url, false, $context);
        $gitrequest = $this->gitprovider->getAuthenticatedRequest(
            'GET',
            'https://api.github.com/user',
            $context
        );
        print_r($gitrequest);
        // return json_decode($gitrequest);
        
    }
    public function profile(){

    }
}

Its giving me 404 error. So how do i go about with the token. Thanks so much for the answers in advance

0 Answers
Related