Laravel 控制器中的 Microsoft Graph API [英] Microsoft Graph API in Laravel Controller

查看:51
本文介绍了Laravel 控制器中的 Microsoft Graph API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用组织 Outlook 帐户.我的目标是在 Laravel 控制器中获取日历事件列表.我遵循的步骤:

  1. 解决方案

    在您的屏幕截图中,它表示您在应用注册时设置的权限尚未获得管理员同意(未授予..." 权限列表中的状态).因为您使用的是客户端凭据,所以应用了应用程序权限(而不是委派的权限).由于尚未获得管理员同意,您的令牌中没有实际权限.

    您需要租户管理员才能转到您的应用注册并授予管理员同意.

    I am using organization Outlook account. My aim is to get List of Calendar Events in the Laravel controller. Steps I followed:

    1. Created simple application in https://aad.portal.azure.com/. I got Application (client) ID, Directory ID, clientSecrets, Object ID/Tenant ID which usable for login and to get further data from Microsoft.

    2. Created controller in the Laravel web.php

        Route::get('/graph', 'microsoftapi@getCalendarData');
    

    1. Added Microsoft Graph SDK for laravel. https://github.com/microsoftgraph/msgraph-sdk-php

      composer require microsoft/microsoft-graph
    

    1. My Laravel Controller code is:

    <?php
    
    namespace App\Http\Controllers;
    
    use App\User;
    use Illuminate\Http\Request;
    use Jumbojett\OpenIDConnectClient;
    use GuzzleHttp\Client;
    use Microsoft\Graph\Graph;
    use Microsoft\Graph\Model;
    
    class microsoftapi extends Controller
    {
        public $accesstoken;
    
        public function __construct()
        {
            $guzzle = new Client();
            $tenantId = 'common';
            $url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';
            $token = json_decode($guzzle->post($url, [
                'form_params' => [
                    'client_id' => '*******************',///$clientId,
                    'client_secret' => '*****************', // $clientSecret,
                    'resource' => 'https://graph.microsoft.com/',
                    'grant_type' => 'client_credentials',
                ],
            ])->getBody()->getContents());
            $this->accessToken = $token->access_token;
    
        }
    
        public function getCalendarData(){
            $graph = new Graph();
            $graph->setBaseUrl("https://graph.microsoft.com/")
                   ->setApiVersion("v1.0")
                   ->setAccessToken($this->accessToken); 
    dd($graph);
            $user = $graph->createRequest("GET","/users/abc@***********.com/calendar/events")
                        ->addHeaders(array("Content-Type" => "application/json"))
                        ->setReturnType(\Microsoft\Graph\Model\User::class)
                        ->setTimeout("1000")
                        ->execute();   
    
            echo "Hello, I am $user->getGivenName() ";
        }
    

    By dd($graph); current output I am getting as:

    Microsoft\Graph\Graph {#221 ▼
      -_accessToken: "eyJ0eXAiOiJKV1QiLCJub25jZSI6IjNueHRoZzZrMllaTVZsRTMzUmNHOVhMbG9TekJRcUhVTzRVY2xLanpGV1EiLCJhbGciOiJSUzI1NiIsIng1dCI6IllNRUxIVDBndmIwbXhvU0RvWWZvbWpxZmpZVSIsImtp ▶"
      -_apiVersion: "v1.0"
      -_baseUrl: "https://graph.microsoft.com/"
      -_proxyPort: null
    }
    

    So I am getting Access token properly. But but, If I commented dd as // dd($graph); then the getCalendarData not working. My output is:

    GuzzleHttp\Exception\ClientException
    Client error: `GET https://graph.microsoft.com/v1.0/users/abc@********.com/calendar/events` resulted in a `401 Unauthorized` response: {"error":{"code":"NoPermissionsInAccessToken","message":"The token contains no permissions, or permissions can not be un (truncated...)
    

    Anyone Know what is the problem ?

    Kindly let me know details.. Please don't send Microsoft links which I am feeling difficult to understand!!!!!

    Please reply me details..

    Permission Screen Shot:

    解决方案

    In your screenshot, it says that admin consent has not been given for the permissions you've set on the app registration (the "Not granted for..." status in the permission list). Because you're using client credentials, the application permissions (not the delegated permissions) are applied. Since admin consent has not been granted, your token has no actual permissions in it.

    You need a tenant admin to go to your app registration and grant admin consent.

    这篇关于Laravel 控制器中的 Microsoft Graph API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆