python-2.7sharepointoffice365adalonedrive

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


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

I am using Azure AD and ADAL Python library to authenticate access to Sharepoint Site file.

import adal
import urllib
import requests
import urllib2

## set variables
username = 'xxx@tenant.onmicrosoft.com'
password = 'xxx$'
authorization_url = 'https://login.windows.net/tenant.onmicrosoft.com' # Authority
redirect_uri = 'https://login.microsoftonline.com/login.srf'
client_id = 'xxx' # 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)}

The authentication is successful as I can do

print access_token

which returns a token string.

I am struggling with the syntax to use 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 have not been able to successfully refer to the file. I am getting an error:

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

This appears to indicate that I am referring to wrong Site. or perhaps referring to folder incorrectly

This is a url that I get from the Sharepoint site for the file I want to download (from its properties in Sharepoint):

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

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


Solution

  • Based on the code, you were authenticate with Azure AD however call the SharePoint REST API. The SharePoint REST is the different authenticate flow. You can refer it from here.

    In your scenario, we can use Microsoft Graph API to download the content from SharePoint site in Office 365. Here is an example that download the content of file on the your default site:

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

    More detail about Microsoft Graph API, please refer to links 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