Same python rest api code cannot authenticate to sharepoint with my personal login information

Luiz Ramos 0 Reputation points
2024-06-22T19:30:24.2066667+00:00

I signed up to Microsoft business yesterday. On the first day, my code would work fine. Now I can't even run this basic code from GitHub with my credentials I know and tested are correct in the browser. I don't have 2fa set up even though it now bugs me about it.

I can find no useful information on this issue.

"ValueError: Cannot get binary security token for from https://login.microsoftonline.com/extSTS.srf"

site_url = "https://xxxx.sharepoint.com"
ctx = ClientContext(site_url).with_credentials(UserCredential(username, password))
web = ctx.web.get().execute_query()
print("Web title: {0}".format(web.properties['Title']))
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,140 questions
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 33,176 Reputation points Microsoft Vendor
    2024-06-24T02:45:11.8466667+00:00

    Hi @Luiz Ramos,

    The default lifetime of Access token is 60 minutes. Your token might be expired in your case. As access tokens are not revoked, so extending the lifetime of access tokens are not recommended as malicious actor that has obtained an access token can use it for extent of its lifetime.

    For maximum security and flexibility, it is recommended to use combination of access token and refresh token. When the access token expires, the application can use the refresh token to obtain the new access token.

    To get the refresh token along with access token and ID tokens, you would need the scope as "offline_access" in your request. The default lifetime of refresh token is valid for 14 days and maximum lifetime is 90 days.

    Reference Articles: https://video2.skills-academy.com/en-us/azure/active-directory-b2c/configure-tokens?pivots=b2c-user-flow

    https://video2.skills-academy.com/en-us/azure/active-directory/develop/active-directory-configurable-token-lifetimes


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

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.