msgraph-sdk-php icon indicating copy to clipboard operation
msgraph-sdk-php copied to clipboard

PHP Uploading to OneDrive fails - Getting "Unable to open file stream for given path error"

Open Zeit42 opened this issue 2 years ago • 4 comments

I'm trying to upload a file to my OneDrive using PHP. I've registered in Microsoft Azure and set the following permissions:

  • Files.Read
  • Files.ReadWrite
  • Files.ReadWrite.All
  • Sites.ReadWrite.All
  • User.Read
  • User.ReadWrite

And here is the chunk of related chunk of PHP Code:

` $tenantId = 'my_tenant_id'; $clientId = 'my_client_id'; $clientSecret = 'my_client_secret';

        $url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token';

        $user_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());
        
        $user_accessToken = $user_token->access_token;

        $general_message = $general_message . "Creating new Graph.";

        $graph = new Graph();
        $graph->setAccessToken($user_accessToken);

        $graph->createRequest("PUT", "/me/drive/root/children/".$_FILES["file"]["name"]."/content")
              ->upload($_FILES["file"]["tmp_name"]);

`

However, I get the following error:

Fatal error: Uncaught Microsoft\Graph\Exception\GraphException: [0]: Unable to open file stream for the given path.

I've also tried the following code snippet found here:

`

use Microsoft\Graph\Graph; use Microsoft\Graph\Model; $guzzle = new \GuzzleHttp\Client(); $tenantId = 'your_tenanet_id, e4c9ab4e----230ba2a757fb'; $clientId = 'your_app_id_registered_in_portal, dc175b96----ea03e56da5e7'; $clientSecret = 'app_key_generated_in_portal, /pGggH*********************Zr732'; $url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token'; $user_token = json_decode($guzzle->post($url, [ 'form_params' => [ 'client_id' => $clientId, 'client_secret' => $clientSecret, 'resource' => 'https://graph.microsoft.com/', 'grant_type' => 'password', 'username' => 'your_user_id, jack@.onmcirosoft.com', 'password' => 'your_password' ], ])->getBody()->getContents()); $user_accessToken = $user_token->access_token;

$graph = new Graph(); $graph->setAccessToken($user_accessToken);

$graph->createRequest("PUT", "/me/drive/root/children/".$_FILES["fileToUpload"]["name"]."/content") ->upload($_FILES["fileToUpload"]["tmp_name"]);

`

However, when I tried this, I got a "interaction needed" error or something similar.

Any ideas? I want the user experience to be as seamless as possible. This means that to the user, they should just interact with my form and their files should be uploaded to OneDrive without having to log in or anything.

Any help is very much appreciated. Thank you! AB#10916

Zeit42 avatar Aug 30 '21 07:08 Zeit42

Any leads?

Zeit42 avatar Sep 14 '21 23:09 Zeit42

In your first example, you are using applications permissions. You cannot use /me since there is not context of /me. Try using the username of the user you want to access with /users/<userIdOrUPN>.

In the second example taken from StackOverflow, it contains some errors and mixing of different auth configurations. That error code hints to me that the tenant admin requires some sort browser based interaction, maybe 2fa.

I suggest you get started with this first to get an app setup to make successful calls to Microsoft Graph: https://github.com/microsoftgraph/msgraph-training-phpapp

MIchaelMainer avatar Sep 16 '21 04:09 MIchaelMainer

In your first example, you are using applications permissions. You cannot use /me since there is not context of /me. Try using the username of the user you want to access with /users/.

In the second example taken from StackOverflow, it contains some errors and mixing of different auth configurations. That error code hints to me that the tenant admin requires some sort browser based interaction, maybe 2fa.

I suggest you get started with this first to get an app setup to make successful calls to Microsoft Graph: https://github.com/microsoftgraph/msgraph-training-phpapp

Any idea where I can find my username? Is it just the email I used without the @domain.com qualifier?

We'll also take a look at the link you sent, thank you.

Zeit42 avatar Oct 13 '21 00:10 Zeit42

@Zeit42 I got the same error when I was trying to upload a file (greater than 4 MB) to Microsoft Graph. With smaller file (< 4 MB) there were no problems to upload. $_FILE variable couldn't read a file location because it was greater than what is allowed in php.ini file. Check your PHP settings (/etc/php/7.x/apache2/php.ini for Debian/Ubuntu). Hope this will bring you some idea.

AlenHujdur avatar Nov 18 '21 21:11 AlenHujdur

No further correspondence from OP. Suggested solutions have been provided:

  • Update php.ini settings
  • Follow training app tutorial

Ndiritu avatar Oct 06 '22 07:10 Ndiritu