Forums

OneDrive Authentication

Hello, I'm using this code from the onedrivesdk module found at

import onedrivesdk
from onedrivesdk.helpers import GetAuthCodeServer

redirect_uri = 'http://localhost:8080/'
client_secret = 'your_app_secret'
scopes=['wl.signin', 'wl.offline_access', 'onedrive.readwrite']
client = onedrivesdk.get_default_client(client_id='your_client_id', scopes=scopes)

auth_url = client.auth_provider.get_auth_url(redirect_uri)

#this will block until we have the code
code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri)

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

The question is: What to put in the URL link redirect_uri ? I have tried user.pythonanywhere.com and www.pythonanywhere.com with no luck.

Thank you for the help.

This code runs in the TASKS section of the site, to run once a day. Not sure if that makes any difference to the question.

im guessing it should be something like https://yourusername.pythonanywhere.com/

Hi, thank you for the response. I had tried it, but doesn’t work.

Any other ideas?

The code works fine in localhost.

Thank you

Ah what happens when it doesn't work? Does it pause / freeze waiting to redirect?

This is the error:

OSError: [Errno 99] Cannot assign requested address

The error comes up when executing this line:

code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri)

I have tried with this two options and still get the error mentioned above:

redirect_uri = 'https://www.pythonanywhere.com'

or

redirect_uri = 'https://user.pythonanywhere.com'

On the OneDriveApp registration website, I have registered the Web App Platform, indicating as ReDirect URLs the same two options mentioned here.

I have a paid account.

Full sequence while running till error

Traceback (most recent call last): File "/home/felixo81/mysite/Test.py", line 23, in <module> code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri) File "/home/felixo81/.local/lib/python3.7/site-packages/onedrivesdk/helpers/GetAuthCodeServer.py", line 60, in get_auth_code s = GetAuthCodeServer((host_address, port), code_acquired, GetAuthCodeRequestHandler) File "/home/felixo81/.local/lib/python3.7/site-packages/onedrivesdk/helpers/GetAuthCodeServer.py", line 76, in init HTTPServer.init(self, server_address, RequestHandlerClass) File "/usr/lib/python3.7/socketserver.py", line 449, in init self.server_bind() File "/usr/lib/python3.7/http/server.py", line 137, in server_bind socketserver.TCPServer.server_bind(self) File "/usr/lib/python3.7/socketserver.py", line 463, in server_bind self.socket.bind(self.server_address) OSError: [Errno 99] Cannot assign requested address

It sounds like it's trying to create some kind of server -- what happens when you run it on localhost? Does it pop up a browser window for you to log in to, or something like that?

Hello, yes, it does what you mentioned. What can I do to make it work?

Check the OneDrive API documentation for an authentication method that is for server processes. They usually have something like that where you can authorise a process locally and then use a token or something like that in your code.