Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authentication OneDrive API Python

Using code sample from GitHub that is specifically for setting up authentication for Python access to OneDrive API (I'm beginning to think this source is outdated), I've failed to make it past the part where you paste code provided by Microsoft after executing program..

Python code:

import onedrivesdk

redirect_uri = 'https://login.microsoftonline.com/common/oauth2/nativeclient'
client_secret = '*this code omitted*'
client_id='*this code omitted*'
api_base_url='https://api.onedrive.com/v1.0/'
scopes=['onedrive.readwrite']

http_provider = onedrivesdk.HttpProvider()
auth_provider = onedrivesdk.AuthProvider(
    http_provider=http_provider,
    client_id=client_id,
    scopes=scopes)

client = onedrivesdk.OneDriveClient(api_base_url, auth_provider, http_provider)
auth_url = client.auth_provider.get_auth_url(redirect_uri)
# Ask for the code
print('Paste this URL into your browser, approve the app\'s access.')
print('Copy everything in the address bar after "code=", and paste it below.')
print(auth_url)
code = raw_input('Paste code here: ')

client.auth_provider.authenticate(code, redirect_uri, client_secret)

After executing code and pasting url in browser, a popup shows up, where I verify that I want to give my app access to API.. I hit "Ok."

I am then presented with code in URL taskbar. I copy and paste code into program..

Then the error I get is:

    raise Exception(str(message["error"]))

Exception: invalid_request

Link to GitHub source used: https://github.com/OneDrive/onedrive-sdk-python

Note: I had to omit scopes such as the first two in this list:

scopes=['wl.signin', 'wl.offline_access', 'onedrive.readwrite']

because they apparently don't exist (according to error code provided by Microsoft after pasting URL into taskbar)

Is there a better source for setting up authentication for a Python program to communicate with OneDrive API?

I am a relatively new Python user, your patience is appreciated.

like image 692
Filip Phigari Avatar asked Oct 26 '25 23:10

Filip Phigari


1 Answers

I ran into the same issue and the solution was to include the redirect_uri in the app registration.

This can be done at https://portal.azure.com/ und Azure Active Directory > App registrations > "Your App" > Authentication. In my case, I needed to add http://localhost:8080/ to the redirect URIs.

I found the suggestion here:

https://github.com/OneDrive/onedrive-sdk-python/issues/98

Hope it helps someone save some time.

like image 66
Justus Hübotter Avatar answered Oct 28 '25 14:10

Justus Hübotter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!