Skip to content

Instantly share code, notes, and snippets.

View isvargasmsft's full-sized avatar

Isaac Vargas isvargasmsft

  • Microsoft
  • San Jose, Costa Rica
View GitHub Profile
@isvargasmsft
isvargasmsft / auth.php
Last active July 10, 2023 21:12
Auth example for PHP SDK 2.0.0-RC5
<?php
// v1.x
$accessToken = getAccessToken(); // custom token retrieval method
$graph = new Graph();
$graph->setAccessToken($accessToken);
$graph ->createRequest('GET', '/me')
->setAccessToken(getAccessToken()) //after initial token expires
...
@isvargasmsft
isvargasmsft / request.php
Last active July 10, 2023 21:11
Request builder pattern for PHP SDK 2.0.0-RC5
<?php
// v1.x
$response = $graphClient->createRequest('GET', '/users/userId/messages')
->setReturnType(Model\User::class)
->execute();
// v2.0-RC
$response = $graphServiceClient->usersById('userId')->messages()->get()->wait();
@isvargasmsft
isvargasmsft / auth.py
Last active October 12, 2023 16:48
Auth example for Python SDK
# Example using async credentials and application access.
from azure.identity.aio import ClientSecretCredential
from msgraph import GraphServiceClient
credential = ClientSecretCredential(
'TENANT_ID',
'CLIENT_ID',
'CLIENT_SECRET',
)
scopes = ['https://graph.microsoft.com/.default']
@isvargasmsft
isvargasmsft / request.py
Last active October 12, 2023 16:49
Request builder pattern for Python SDK
client = GraphServiceClient(credentials=credential, scopes=scopes)
# GET /users/{id | userPrincipalName}
async def get_user():
user = await client.users.by_user_id('userPrincipalName').get()
@isvargasmsft
isvargasmsft / auth2.php
Last active October 18, 2023 11:58
Authentication with the Microsoft Graph PHP SDK 2.0.0
<?php
use Microsoft\Graph\GraphRequestAdapter;
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Kiota\Abstractions\ApiException;
use Microsoft\Kiota\Authentication\PhpLeagueAuthenticationProvider;
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
$tokenRequestContext = new ClientCredentialContext(
'tenantID',
'clientID',
@isvargasmsft
isvargasmsft / pageiterator.php
Last active November 8, 2023 12:31
Paging through a collection
<?php
use Microsoft\Graph\Core\Tasks\PageIterator;
use Microsoft\Graph\Generated\Models\Message;
use DateTimeInterface;
$messages = $graphServiceClient->users()->byUserId(USER_ID)->messages()->get()->wait();
$pageIterator = new PageIterator($messages, $graphServiceClient->getRequestAdapter());
$counter = 0;
@isvargasmsft
isvargasmsft / largefileupload.php
Last active November 8, 2023 12:35
Uploading large files
<?php
use Psr\Http\Client\NetworkExceptionInterface;
// create a file stream
$file = Utils::streamFor(fopen('fileName', 'r'));
// create an upload session
$attachmentItem = new AttachmentItem();
$attachmentItem->setAttachmentType(new AttachmentType('file'));
$attachmentItem->setName('fileName');
@isvargasmsft
isvargasmsft / batching.php
Last active November 8, 2023 13:01
Batching requests
<?php
use Microsoft\Graph\Core\Requests\BatchRequestContent;
use Microsoft\Graph\Generated\Models\Message;
use Microsoft\Graph\BatchRequestBuilder;
use Microsoft\Graph\Core\Requests\BatchResponseItem;
use Microsoft\Graph\Generated\Models\Message;
// Create a batch request content object
$message = new Message();
@isvargasmsft
isvargasmsft / request2.php
Last active July 10, 2023 21:11
Fluent experience in the 2.0 SDK
<?php
// v1.x
$response = $graphClient->createRequest('GET', '/users/userId/messages')
->setReturnType(Model\User::class)
->execute();
// v2.0.0
$response = $graphServiceClient->users()->byUserId('userId')->messages()->get()->wait();
@isvargasmsft
isvargasmsft / init_rc19.php
Created July 10, 2023 21:17
Initialization in RC19 vs RC5
<?php
//RC5 initialization experience
$tokenRequestContext = new ClientCredentialContext(
'tenantID',
'clientID',
'clientSecret'
);
$scopes = ['https://graph.microsoft.com/.default'];
$authProvider = new PhpLeagueAuthenticationProvider($tokenRequestContext, $scopes);