dcblogdev / laravel-microsoft-graph

Laravel package for Microsoft Graph API (Microsoft365)
https://dcblog.dev/docs/laravel-microsoft-graph
Other
120 stars 51 forks source link

error 90: Undefined property: stdClass::$access_token #48

Closed Kristianmarku closed 1 year ago

Kristianmarku commented 1 year ago

Dear @dcblogdev ,

I hope this message finds you well. I recently came across an issue while using your repository and wanted to bring it to your attention.

The error I encountered was: " error 90: Undefined property: stdClass::$access_token] ". This happens when this function is executed

public function connect()
    {
       return MsGraphAdmin::connect();
    }

I've set up everything as instructed by your documentation. I'm not sure whether the error is happening on the repository side or in my Azure Active Directory App Registration.

I would greatly appreciate it if you could look into this issue and let me know if there is a solution or workaround. I have done research on this error but so far, I have been unable to resolve it.

Thank you for your time and for maintaining this repository. I am a big fan of your work and I appreciate all the effort you have put into it.

Best regards, Kristian Marku.

dcblogdev commented 1 year ago

thanks for reporting this, looks to be a problem with the API structure, not sure if there's been changes from Microsoft.

It seems that it no longer accepts a resource type when authenticating as a tenant, I've replicated this locally and working on testing it deeper before issuing a release.

Thank you for your time and for maintaining this repository. I am a big fan of your work and I appreciate all the effort you have put into it.

thank you, its great to hear its been useful to you.

Kristianmarku commented 1 year ago

thanks for reporting this, looks to be a problem with the API structure, not sure if there's been changes from Microsoft.

It seems that it no longer accepts a resource type when authenticating as a tenant, I've replicated this locally and working on testing it deeper before issuing a release.

Thank you for your time and for maintaining this repository. I am a big fan of your work and I appreciate all the effort you have put into it.

thank you, its great to hear its been useful to you.

I truly hope that you can find a solution to fix this issue soon. As I mentioned, I need this package for a project and it would be a huge setback for me if I can't use it. I will definitely keep an eye out for updates and hope that you can resolve this as soon as possible. Thank you again for all your hard work and for providing such a valuable resource.

dcblogdev commented 1 year ago

I've just pushed up a new release now.

Kristianmarku commented 1 year ago

I've updated the repository in my project, and now when this function is executed:

public function connect()
    {
       return MsGraphAdmin::connect();
    }

The website just returns json data!? Though I've set up routes for when user is authenticated via Microsoft, to return to a blade view which fetches some user data.

image

Is the issue on my project side or If I could get some help...?

dcblogdev commented 1 year ago

with the new change added, connect doesn't need to be loaded directly after you've initially connected. Once you have a token a token would be requested when needed.

take this example:

Route::group(['middleware' => ['web', 'MsGraphAdminAuthenticated']], function(){
    Route::get('/', function(){
        //fetch all users
        return MsGraphAdmin::get('users');
    });
});

Route::get('connect', function(){
    return MsGraphAdmin::connect();
});

I'm loading users on a / route internally called connect when needed. If I manually go to the route then I see the same output as you see above.

Kristianmarku commented 1 year ago

When the user clicks on the "Login" button, it redirects him to the route "connect", which before authenticated the user, and then the view blade was returned, now It just shows json data in the web. So how do I authenticate the user first, then redirect him to my view?

When I use MsGraph, instead of Admin, then when MsGraph::connect is returned, it sends the me to the Microsoft Login Form, and select an account then it authenticates me and redirects me to my view blade with the user data. But with MsGraphAdmin, instead of sending to Microsoft Login Form, it just prints the json data, the image that I uploaded above shows it.

Web routes:

Route::get('/', function () {
    return view('auth.login');
})->name('login');

Route::group(['middleware' => ['web', 'auth']], function(){
    Route::post('meeting', [AuthController::class, 'storeMeeting'])->name('meeting.store');
});

Route::group(['middleware' => ['web', 'guest'], 'namespace' => 'App\Http\Controllers'], function(){
    Route::get('msgraph', 'AuthController@index')->name('index');
    Route::get('msgraph/oauth', 'AuthController@connect')->name('connect');
});

AuthController:

<?php

namespace App\Http\Controllers;

use Dcblogdev\MsGraph\Facades\MsGraphAdmin;
use Illuminate\Http\Request;

class AuthController extends Controller
{
    public function index()
    {
        if (! is_string(MsGraphAdmin::getAccessToken())) {
            return redirect(env('MSGRAPH_OAUTH_URL'));
        } else {
            // Display Details
            $user = MsGraphAdmin::get('me');
            $contacts = MsGraphAdmin::get('me/contacts');
            $events = MsGraphAdmin::get('me/events');
            $teams = MsGraphAdmin::get('me/joinedTeams');

            $meetings = [];
            foreach ($teams['value'] as $team) {
                $teamMeetings = MsGraphAdmin::get("teams/{$team['id']}/events");
                $meetings = array_merge($meetings, $teamMeetings['value']);
            }

            return view('welcome', compact('user', 'contacts', 'events', 'meetings'));
        }
    }

    public function connect()
    {
        return MsGraphAdmin::connect();
    }

    public function logout()
    {
        return MsGraphAdmin::disconnect('/');
    }

    public function storeMeeting(Request $request)
    {
        if (! MsGraphAdmin::isConnected()) {
            return redirect('msgraph/oauth');
        }

        $subject = $request->input('subject');
        $start_time = $request->input('start_time');
        $end_time = $request->input('end_time');

        $data = [
            'subject' => $subject,
            'start' => [
                'dateTime' => $start_time,
                'timeZone' => 'UTC',
            ],
            'end' => [
                'dateTime' => $end_time,
                'timeZone' => 'UTC',
            ],
        ];

        $response = MsGraphAdmin::post('/me/onlineMeetings', $data);

        return redirect()->back();
    }
}
dcblogdev commented 1 year ago

I understand, I'm working on a way this can be used as both in the background and via user interaction.

stretch that I've pushed a new release where connect will redirect by default. To stop this pass false as a param and only the token would be returned.

I do want to clean up the internal code its getting pretty messy.