Hi,
It sounds like your access token itself is valid, but the call is being blocked by a policy or user context requirement in the tenant, especially since it starts working as soon as you sign in via the SharePoint UI. This pattern lines up with Microsoft Entra (Azure AD) Conditional Access session controls, such as sign‑in frequency, which can override default token lifetimes and require users (including guests) to reauthenticate after a configured interval.
A few things to check:
1. Conditional Access / sign‑in policies
In the tenant (and in the external tenant where you’re a guest), ask an admin to check Entra ID → Security → Conditional Access for any policies that:
- Require MFA
- Require a compliant device
- Enforce a sign‑in frequency (e.g. reauthenticate every X hours)
Microsoft’s docs explain that these session controls apply to cloud apps like SharePoint Online and Microsoft Graph and can force reauthentication after a set window(see Manage Conditional Access session controls). In practice, that means non‑interactive API calls can start failing with accessDenied until the user does another interactive sign‑in in the browser.
2. Guest user behavior
Since you’re a guest in another tenant, confirm:
- That your guest invitation has been accepted and the account is fully set up.
- Whether that tenant has specific Conditional Access rules for guest users, or expects guests to sign into SharePoint at least once before programmatic access works.
Guest accounts are subject to the same Conditional Access and session policies as internal users, so sign‑in frequency and other session controls can also force guests to periodically reauthenticate via the browser before delegated API calls will succeed (again, see the Conditional Access session lifetime and sign‑in frequency docs linked above).
3. Token details (to rule out simple issues)
Decode your access token and check:
-
aud = https://graph.microsoft.com if you’re calling Graph, or https://{tenant}.sharepoint.com if you’re using the SharePoint REST API directly.
-
scp / roles include the expected file/SharePoint permissions.
If there is a sign‑in frequency Conditional Access policy, the behavior you describe (API works for some hours after visiting the site, then only works again after logging in via UI) is expected. In that case, you’ll need to either:
- Have an admin adjust the Conditional Access / session policy for this scenario, or
- Ensure your application uses an interactive OAuth flow that can satisfy Conditional Access requirements and then rely on refresh tokens within that window. Microsoft’s developer guidance for Conditional Access describes how apps using OAuth/OpenID Connect are expected to handle these policies.
Hope this helps.