2
votes

I trying to upload a simpale text as file to oneDrive using REST API.

I Create App inportal.azure.com And i add the follow API permissions enter image description here

I create a token: https://login.microsoftonline.com/< My tenantName>/oauth2/v2.0/token Send the follow pramters:

Content-Type application/x-www-form-urlencoded Keep-Alive true

I send it as a post and get a JSON with the token.

Then I try to upload the text as file.

using the follow URL https://graph.microsoft.com/v1.0/drive/root://test.txt:/content

  • authorization Bearer {my token String}

  • Content-Type text/plain

  • Body "The string that needs to upload"

    { "error": { "code": "BadRequest", "message": "Unable to retrieve tenant service info.", "innerError": { "request-id": "098f37d5-96fd-44d0-905b-c147eac223f5", "date": "2020-05-19T11:43:03" } } }

enter image description here

What I do Wrong?

Thanks in advance

3

3 Answers

0
votes

If you want to use Microsoft Graph to upload file to onedrive, please refer to the document

PUT /drives/{drive-id}/items/{parent-id}:/{filename}:/content
PUT /users/{user-id}/drive/items/{parent-id}:/{filename}:/content

For example, upload file to one user's onedrive in your tenant

  1. Create Azure AD application

  2. Configure Permissions for the application. (Microsoft graph -> Application Permissions -> Files.ReadWrite.All, Sites.ReadWrite.All)

  3. Get access token

Post  https://login.microsoftonline.com/< My tenantName>/oauth2/v2.0/token
Content-Type application/x-www-form-urlencoded 

grant_type= client_credentials
&client_id= My client id
&client_secret= My client secret
&scope =https://graph.microsoft.com/.default

4 upload file to one user's onedrive

Put https://graph.microsoft.com/v1.0/users/<user object id or upn>/drive/items/root:/test.txt:/content
Authorization : Bearer <Access token>

Content-Type: text/plain


<file content>

enter image description here

Besides, please note that when we use API to upload file, the size of file cannot be larger than 4MB. If you want to upload large file, please refer to the document and the document

0
votes

In case anyone else has a similar issue, a few days ago, I had the same problem, and I partially solve it by creating a tenant in Azure Active Directory(ADD) and switching to this new one. But with this new tenant for personal Microsoft accounts, I had the "Tenant does not have a SPO license." error. This happened because ADD app type was wrong, as well as my endpoints, to use with a personal Microsft account.

If you have a business account, you should use the account with office 365 business subscription and configure your tenant to use it. Like this will work as you configured.

If you have a personal Microsoft account, I shared a step-by-step how to solve this problem and the limitations for our account type here.

-1
votes

To upload file (<4MB in size) to your OneDrive in the tenant.

This command will do the job

foo@bar:~$ ACCESS_TOKEN=eyJ0eXAiOiJKV1QiLCJub25jZSI6IlZ0MVBN...
foo@bar:~$ curl -X PUT 'https://graph.microsoft.com/v1.0/me/drive/root:/myNewFolder/file.tgz:/content' -H 'Authorization: Bearer '"${ACCESS_TOKEN}"'' -T test.tgz