如何在此 Laravel 应用程序中从 Microsoft Graph 获取当前用户的图片?

How can I get the current user's picture from Microsoft Graph in this Laravel app?

我正在开发 Laravel 8 应用程序,该应用程序使用 Microsoft Azure 进行用户管理(包括登录)。

我开始在他们的网站上关注 this tutorial

我在尝试显示当前用户的图片时卡住了。

在我看来:

<img src="{{ isset($user_avatar) ? $user_avatar : asset('img/default-avatar.png') }}" class="rounded-circle avatar-top">

AuthController 控制器中,我得到 当前用户 的数据:

$user = $graph->createRequest('GET', '/me? 
$select=displayName,mail,mailboxSettings,userPrincipalName,givenName,surname,photo')
      ->setReturnType(Model\User::class)
      ->execute();

app\Http\Controllers\Controller.php 我有:

class Controller extends BaseController {
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
        
    public function loadViewData() {

        $viewData = [];

        // Check for flash errors
        if (session('error')) {
            $viewData['error'] = session('error');
            $viewData['errorDetail'] = session('errorDetail');
        }

        // Check for logged on user
        if (session('userName'))
        {
            $viewData['userName'] = session('userName');
            $viewData['firstName'] = session('firstName');
            $viewData['lastName'] = session('lastName');
            $viewData['user_avatar'] = session('userAvatar');
            $viewData['userEmail'] = session('userEmail');
            $viewData['userTimeZone'] = session('userTimeZone');
        }

        return $viewData;
    }
}

在 DashboardContoller 中,我将 $viewData 传递给视图:

class DashboardContoller extends Controller
{
    public function index(){
        $viewData = $this->loadViewData();
        return view('dashboard', $viewData);
    }
}

app\TokenStore\TokenCache.php我有:

public function storeTokens($accessToken, $user) {
 session([
  'accessToken' => $accessToken->getToken(),
  'refreshToken' => $accessToken->getRefreshToken(),
  'tokenExpires' => $accessToken->getExpires(),
  'userName' => $user->getDisplayName(),
  'firstName' => $user->getGivenName(),
  'lastName' => $user->getSurname(),
  // Set AVATAR
  'userAvatar' => $user->getPhoto(),
  'userEmail' => null !== $user->getMail() ? $user->getMail() : $user->getUserPrincipalName(),
  'userTimeZone' => $user->getMailboxSettings()->getTimeZone()
 ]);
}

如上所示,我使用 User 模型 (vendor\microsoft\microsoft-graph\src\Model\User.php) 中的 getPhoto() 方法。

成功这样显示了用户的全名

问题:

由于我一直无法弄清楚的原因,user_avatar returns null.

我的错误是什么?

User 资源类型上的 Photo 属性 始终是 null,这是一个已知问题。目前图表 API 不支持将 SelectPhoto 属性 一起使用。

要获取照片的内容,您必须调用另一个端点

GET /me/photo/$value
GET /users/{id | userPrincipalName}/photo/$value

添加到AuthController

$photo = $graph->createRequest("GET", "/me/photo/$value")
       ->execute();
$photoMeta = $graph->createRequest("GET", "/me/photo")
       ->execute();

userAvatar

更新 storeTokens 函数
public function storeTokens($accessToken, $user, $photo, $photoMeta) {
 $body = $photo->getRawBody();
 $base64 = base64_encode($body);
 $meta = $photoMeta->getBody();
 $mime = $meta["@odata.mediaContentType"]
 $img = ('data:' . $mime . ';base64,' . $base64);
 session([
  'accessToken' => $accessToken->getToken(),
  'refreshToken' => $accessToken->getRefreshToken(),
  'tokenExpires' => $accessToken->getExpires(),
  'userName' => $user->getDisplayName(),
  'firstName' => $user->getGivenName(),
  'lastName' => $user->getSurname(),
  // Set AVATAR
  'userAvatar' => $img,
  'userEmail' => null !== $user->getMail() ? $user->getMail() : $user->getUserPrincipalName(),
  'userTimeZone' => $user->getMailboxSettings()->getTimeZone()
 ]);
}

资源:

Get profilePhoto

How to include photo

Graph API support for Photos property in select