Laravel Controller中的Microsoft Graph API [英] Microsoft Graph API in Laravel Controller

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

问题描述

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

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

  1. https://aad.portal.azure.com/.我获得了可用于登录并从Microsoft获得更多数据的应用程序(客户端)ID,目录ID,clientSecrets,对象ID/租户ID.

  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.

在Laravel web.php中创建的控制器

Created controller in the Laravel web.php

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

  1. 为laravel添加了Microsoft Graph SDK. https://github.com/microsoftgraph/msgraph-sdk-php

  composer require microsoft/microsoft-graph

  1. 我的Laravel控制器代码为:

<?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() ";
    }

通过dd($graph);当前输出,我得到的是:

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
}

所以我正确地获得了访问令牌.但是,但是,如果我将dd注释为// dd($graph);,则getCalendarData无法正常工作.我的输出是:

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 ?

请让我知道详细信息..请不要发送我感觉很难理解的Microsoft链接!!!!

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

请回复我的详细信息.

权限截屏:

推荐答案

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

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 Controller中的Microsoft Graph API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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