Skip to content

Latest commit

 

History

History
104 lines (69 loc) · 2.07 KB

picker.md

File metadata and controls

104 lines (69 loc) · 2.07 KB

Picker API

To handle files other than those uploaded via the API, you must use the Picker API.

Picker API requires an Access Token

Get an access token from the refresh token

use Revolution\Google\Photos\Support\Token;

$token = Token::toAccessToken('refresh_token');

session(['picker_token' => $token]);

Mock Token::toAccessToken() in testing

You can use Token::fake() to return a fixed token from Token::toAccessToken().

use Revolution\Google\Photos\Support\Token;

Token::fake(token: 'test');

$token = Token::toAccessToken('refresh_token');
// test

Reset mock

use Revolution\Google\Photos\Support\Token;

Token::fake(token: null);

Session create

use Revolution\Google\Photos\Facades\Picker;

$picker = Picker::withToken(session('picker_token'))->create();

session(['picker' => $picker]);

dump($picker);
// PickingSession
//[
//    'id' => '...',
//    'pickerUri' => 'https://',
//    'pollingConfig' => [],
//    'mediaItemsSet' => false,
//]

pickerUri opens a new browser and the user can select photos.

<a href="{{ session('picker.pickerUri') }}" target="_blank"></a>

There is no way to return to the original URL from Google Photos.

Session get

Since it does not return to the original URL, you have no choice but to check it on the Laravel side.

use Revolution\Google\Photos\Facades\Picker;

$picker = Picker::withToken(session('picker_token'))->get(session('picker.id'));

session(['picker' => $picker]);

dump($picker);
// PickingSession
//[
//    'id' => '...',
//    'pickerUri' => 'https://',
//    'pollingConfig' => [],
//    'mediaItemsSet' => true,
//]

If mediaItemsSet is true, the user has finished selecting photos.

MediaItems list

Finally, you can get a list of photos selected by the user.

use Revolution\Google\Photos\Facades\Picker;

$list = Picker::withToken(session('picker_token'))->list(session('picker.id'));

dump($list);

//[
//    'mediaItems' => [],
//    'nextPageToken' => '...',
//]