'Getting authenticated to personal OneDrive

am trying to get authenticated with a Python to OneDrive (personal, not for business). I've registered an app in Azure AD, got client id and secret (turned to be not needed, as I am using 'desktop' app which is public and not using a secret), and Using browser and postman, managed to obtain an access token.

trying 2 different options now, both with no luck.

import hidden
from hidden import oauthr
import requests
import json


client_secret = oauthr()["consumer_secret"]
client_id = oauthr()["consumer_key"]
scope = 'Files.ReadWrite.All'
redirect_uri = "http://localhost/auth-response"
code = oauthr()["code"]
token = oauthr()["token_secret"]


RootFolder = 'https://api.onedrive.com/v1.0/drive/root:/'

r = requests.get(RootFolder, headers = {'Authorization': 'Bearer ' + token})

content=json.loads(r.content)
print(content)

This one results in: {'error': {'code': 'unauthenticated', 'message': 'Authentication failed'}}

Second thing I try is MSAL:

import hidden
from hidden import oauthr
import requests
import json

from msal import PublicClientApplication


client_secret = oauthr()["consumer_secret"]
client_id = oauthr()["consumer_key"]
scopes = ['https://graph.microsoft.com/.default']
redirect_uri = "http://localhost/auth-response"
code = oauthr()["code"]
token = oauthr()["token_secret"]

user = input("user: ")
pwd = input("pwd: ")
print('scopes are: ', scopes, ' the data type is: ',type(scopes) )

app = PublicClientApplication(
    client_id,
    authority="https://login.microsoftonline.com/UsadyProgimnasia.onmicrosoft.com")
    
result = None

flow = app.initiate_device_flow(scopes = scopes)
    
accounts = app.get_accounts()
if accounts:
    # If so, you could then somehow display these accounts and let end user choose
    print("Pick the account you want to use to proceed:")
    for a in accounts:
        print(a["username"])
    # Assuming the end user chose this one
    chosen = accounts[0]
    # Now let's try to find a token in cache for this account
    result = app.acquire_token_silent([scopes], account=chosen)

   
if not result:
    print('So no suitable token exists in cache. Let\'s get a new one from Azure AD')
    #result = app.acquire_token_by_username_password(user, pwd, scopes)
    result = app.acquire_token_by_authorization_code(code, scopes, redirect_uri=redirect_uri, nonce=None, claims_challenge=None)
    #result = app.acquire_token_by_device_flow(flow, claims_challenge=None)
    if "access_token" in result:
        print(result["access_token"])  # Yay!
    else:
        print(result.get("error"))
        print(result.get("error_description"))
        print(result.get("correlation_id"))  # You may need this when reporting a bug

This gives a invalid_grant AADSTS70000121: The passed grant is from a personal Microsoft account and is required to be sent to the /consumers or /common endpoint.

Would appreciate an advice, chaps

Regards



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source