microsoftgraph / msgraph-sdk-python

MIT License
388 stars 56 forks source link

Cannot get mail items for a shared mailbox, fetching user data works(?) fine #336

Closed jussihi closed 1 year ago

jussihi commented 1 year ago

I cannot seem to get Message items out of a shared mailbox user.

Here is my full code snippet:

import asyncio

from azure.identity.aio import ClientSecretCredential
from msgraph import GraphServiceClient
from msgraph.generated.directory_role_templates.delta.delta_request_builder import DeltaRequestBuilder
from msgraph.generated.models.user_collection_response import UserCollectionResponse

CLIENT_ID = ",,,"
CLIENT_SECRET = "..."
TENANT_ID = "..."

credential = ClientSecretCredential(tenant_id=TENANT_ID,
                                    client_id=CLIENT_ID,
                                    client_secret=CLIENT_SECRET)
scopes = ['https://graph.microsoft.com/.default']

client = GraphServiceClient(credentials=credential, scopes=scopes)

request_configuration = DeltaRequestBuilder.DeltaRequestBuilderGetRequestConfiguration(
headers = {
        'Prefer' : "odata.maxpagesize=2",
    }
)

async def main():
    user: UserCollectionResponse = await (client.users.by_user_id("...").get())
    print(user)
    msgs= await user.messages # This results in None

asyncio.run(main())

it works fine, I can get the user information just fine. However, printing out the user information shows "None" as most of the values for the fields:

User(additional_data={'@odata.context': 'https://graph.microsoft.com/v1.0/$metadata#users/$entity'}, id='....', odata_type='#microsoft.graph.user', deleted_date_time=None, about_me=None, account_enabled=None, activities=None, age_group=None, agreement_acceptances=None, app_role_assignments=None, assigned_licenses=None, assigned_plans=None, authentication=None, authorization_info=None, birthday=None, business_phones=[], calendar=None, calendar_groups=None, calendar_view=None, calendars=None, chats=None, city=None, company_name=None, consent_provided_for_minor=None, contact_folders=None, contacts=None, country=None, created_date_time=None, created_objects=None, creation_type=None, custom_security_attributes=None, department=None, device_enrollment_limit=None, device_management_troubleshooting_events=None, direct_reports=None, display_name='Test-shared mailbox', drive=None, drives=None, employee_experience=None, employee_hire_date=None, employee_id=None, employee_leave_date_time=None, employee_org_data=None, employee_type=None, events=None, extensions=None, external_user_state=None, external_user_state_change_date_time=None, fax_number=None, followed_sites=None, given_name=None, hire_date=None, identities=None, im_addresses=None, inference_classification=None, insights=None, interests=None, is_resource_account=None, job_title=None, joined_teams=None, last_password_change_date_time=None, legal_age_group_classification=None, license_assignment_states=None, license_details=None, mail='....@.....onmicrosoft.com', mail_folders=None, mail_nickname=None, mailbox_settings=None, managed_app_registrations=None, managed_devices=None, manager=None, member_of=None, messages=None, mobile_phone=None, my_site=None, oauth2_permission_grants=None, office_location=None, on_premises_distinguished_name=None, on_premises_domain_name=None, on_premises_extension_attributes=None, on_premises_immutable_id=None, on_premises_last_sync_date_time=None, on_premises_provisioning_errors=None, on_premises_sam_account_name=None, on_premises_security_identifier=None, on_premises_sync_enabled=None, on_premises_user_principal_name=None, onenote=None, online_meetings=None, other_mails=None, outlook=None, owned_devices=None, owned_objects=None, password_policies=None, password_profile=None, past_projects=None, people=None, photo=None, photos=None, planner=None, postal_code=None, preferred_data_location=None, preferred_language=None, preferred_name=None, presence=None, print=None, provisioned_plans=None, proxy_addresses=None, registered_devices=None, responsibilities=None, schools=None, scoped_role_member_of=None, security_identifier=None, settings=None, show_in_address_list=None, sign_in_activity=None, sign_in_sessions_valid_from_date_time=None, skills=None, state=None, street_address=None, surname=None, teamwork=None, todo=None, transitive_member_of=None, usage_location=None, user_principal_name='....@.....onmicrosoft.com', user_type=None)

I've given my shared mailbox the required license (e5). I've also given my application Application permissions to

Mail.Read
Mail.ReadBasic 
Mail.ReadBasic.All
Mail.ReadWrite
User.Read.All

What am I missing? I'm trying to follow your docs but with a shared mailbox.

jussihi commented 1 year ago

Nvm, found the problem. It seems like I cannot reuse a user instance once retrieved the UserCollectionResponse with get, I need to fetch everything at once:

messages = await (client.users.by_user_id("...").messages.get(request_configuration=request_configuration))

This fixed my issue. Closing this issue.