Can't access onedrive in another tenant

Jan C 20 Reputation points

I want to create an aplication that will access and read all data from onedrive in another tenant.
The app will act like a daemon in backgroud, without any user intervention. I want to use MIcrosfot Graph API to access data in onedrive.

Naming convention:
X - my tenant, where my application is registered
Y - some other tenant whose onedrive I want to be accessible by my application in my tenant X

I use such scopeimage

And in my python code i use scopes=".defualt" and client msal.ConfidentialClientApplication.
Code for authorization:

client_app_auth = ConfidentialClientApplication(
    authority="" + TENANT_ID,
def save_refresh_token(refresh_token: str):
    with open(REFRESH_TOKEN_PATH, "w", encoding="utf-8") as file:
        file.write(json.dumps({"refresh_token": refresh_token}))
def login():
    url = client_app_auth.get_authorization_request_url(
    return {"login_url": url}
def auth_callback(code: str):
    result = client_app_auth.acquire_token_by_authorization_code(
    if "access_token" in result:
        return {"message": "Authentication successful", "results": result}
        return {"message": "Authentication failed", "details": result.get("error")}

Code for listing all users drives:

async def get_all_drive():
    with open(REFRESH_TOKEN_PATH, "r", encoding="utf-8") as file:
        refresh_token = json.load(file)["refresh_token"]
    auth_token = client_app_auth.acquire_token_by_refresh_token(
        refresh_token=refresh_token, scopes=SCOPES
    if "access_token" in auth_token:
        access_token = auth_token["access_token"]
        graph_endpoint = "" + USER_ID + "/drive"
        headers = {
            "Authorization": f"Bearer {access_token}",
            "Content-Type": "application/json",
        user_response = requests.get(graph_endpoint, headers=headers)
        user_data = user_response.json()
        return user_data

This is configuration of authenticatoin in tenant X:
User's image

Program works fine when I login with my tenant X acccount (also global admin). I'm able to list drives and then use driveId to list files in it.
BUT, when i try to login with Y account i get such error:
User's image

I'm trying to login with Global Admin account from tenant Y, so I dont undertstand why would i need some admin approval if I AM the admin.
I've added account from tenant Y to tenant X as a Guest/ExternalAzureAD.

Could somebody help me undestand where I'm making the mistake and how to fix it?

Or maybe I'm approching my endgoal from wrong angle altogether?

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,140 questions
A Microsoft file hosting and synchronization service.
920 questions
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,078 questions
0 comments No comments
{count} votes

Accepted answer
  1. AsithwMSFT 1,420 Reputation points Microsoft Vendor

    Hello Jan C

    To create an application that accesses and reads data from OneDrive in another tenant using the Microsoft Graph API without user interaction, you'll need to follow the client credential flow.

    It appears that you have successfully registered an Azure AD app and obtained a client ID and client secret, and also confirmed the required scopes. Here, only application scopes are needed. (no delegated scopes)

    The next step is to generate a consent URL for Tenant Y

    your application will act on behalf of a tenant without a signed-in user. An administrator from the tenant Y must grant consent to your application to access their OneDrive data

    Here is the format for the consent URL. The admin of Tenant Y should use this URL to grant consent to your app:


    Once this is successfully done, it will redirect to the redirect_url. Here is the format:


    If admin_consent=True, it means consent is successful.

    For more details, refer to this link.

    Now, you can query Tenant Y’s OneDrive data using the client credential flow.

    If you wish to use the Python GraphClient, here is a sample for you:

    class Graph:
        settings: SectionProxy
        client_credential: ClientSecretCredential
        app_client: GraphServiceClient
        def __init__(self, config: SectionProxy):
            self.settings = config
            client_id = self.settings['clientId']
            tenant_id = self.settings['tenantId']
            client_secret = self.settings['clientSecret']
            self.client_credential = ClientSecretCredential(tenant_id, client_id, client_secret)
            self.app_client = GraphServiceClient(self.client_credential) # type: ignore
        async def get_app_only_token(self):
            graph_scope = ''
            access_token = await self.client_credential.get_token(graph_scope)
            return access_token.token
        async def make_one_drive_graph_call(self):

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.

0 additional answers

Sort by: Most helpful