OneDrive API - see Sharepoint file to upload or download - invalid audience error

I want to programmatically interact with files in the Office 365 E3 Sharepoint Site.

I am using Azure AD and the ADAL Python library to authenticate access to a SharePoint site file.

import adal import urllib import requests import urllib2 ## set variables username = ' curtis@tenant.onmicrosoft.com ' password = 'OFoarry8Oe$' authorization_url = 'https://login.windows.net/tenant.onmicrosoft.com' # Authority redirect_uri = 'https://login.microsoftonline.com/login.srf' client_id = 'dcbf844f-d2c3-42d1-8a7d-0f838f57899a' # Client id ## use ADAL to create token response token_response = adal.acquire_token_with_username_password( authorization_url, username, password ) ## endpoints discovery ## https://api.office.com/discovery/v2.0/me/allServices ## create refresh token and save it to use later refresh_token = token_response['refreshToken'] refresh_token_file = open('refresh_token.txt', 'w') refresh_token_file.write(refresh_token) refresh_token_file.close() ## get saved refresh token and use it to get new token response refresh_token = open('refresh_token.txt', 'r').read() token_response = adal.acquire_token_with_refresh_token(authorization_url, str(refresh_token)) ## get access_token from token response access_token = token_response.get('accessToken') headers = {'Authorization':'BEARER ' + str(access_token)} 

Authentication was successful, how can I do

 print access_token 

which returns a token string.

I am struggling with the syntax used to download and upload files from a Sharepoint folder. This is what I have so far:

 ## download file file_url = 'https://tenant.sharepoint.com/_api/v1.0/files/root:/myfoldername/myfilename.csv:/content' r = requests.get(file_url, headers=headers) print r.text 

So far, I could not successfully link to the file. I get an error message:

 {"error":"invalid_client","error_description":"Invalid audience Uri 'https:\/\/management.core.windows.net\/'."} 

This means that I mean the wrong site . or incorrectly referring to

This is the URL that I get from the Sharepoint site for the file I want to upload (from its properties in Sharepoint):

 https://tenant.sharepoint.com/Shared%20Documents/myfoldername/myfilename.csv 

Does the url of a file from a Sharepoint site determine file_url syntax? If not, how else can I determine what file_url should be?

0
source share
2 answers

Based on the code, you authenticated with Azure AD, but you are accessing the SharePoint REST API. SharePoint REST is a different authentication stream. You can send it from here .

In your scenario, we can use the Microsoft Graph API to upload content from a SharePoint site to Office 365. The following is an example of uploading file contents to your site by default:

 GET: https://graph.microsoft.com/v1.0/me/drive/root:/test.txt:/content authorization: bearer {token} 

For more information on the Microsoft Graph API, see below:

https://graph.microsoft.io/en-us/docs/api-reference/v1.0/api/item_downloadcontent

https://graph.microsoft.io/en-us/docs/authorization/app_authorization

0
source

Fei Xue set me on the right track. My code requested a resource that ADAL did not expect.

Turns adal.acquire_token_with_username_password in __init__.py has default values (see bottom of the code in class _DefaultValues ) for client_id and resource .

The default ADAL resource is https://management.core.windows.net/ , which was expected by my file_url resource. invalid audience was https://tenant.sharepoint.com .

So, I changed the default ADAL values:

 `client_id` = my Azure AD app client_id `resource` = `https://tenant.sharepoint.com/` 

ADAL acquire_token_with_username_password (see below) has client_id and resource set to None . I have not tried, but I think they can be edited to remove =None and set in my code instead of class _DefaultValues .

 def acquire_token_with_username_password( authority, username, password, client_id=None, resource=None, validate_authority=True ): 

And also made a minor (but mandatory) change to my file_url (url to filename) to:

 file_url = 'https://pokrant.sharepoint.com/_api/v2.0/drive/root:/analytics/output_analytics.csv:/content' 

Now, when I run the code, I get the contents of the csv content printed on the console.

0
source

All Articles