microsoftgraph / msgraph-sdk-go

Microsoft Graph SDK for Go
https://docs.microsoft.com/en-us/graph/sdks/sdks-overview
MIT License
214 stars 32 forks source link

The token used for this request is expected to be at least 45 minutes away from expiry. #702

Closed macnibblet closed 1 month ago

macnibblet commented 2 months ago

Given the following function where we cache the creation of the client to avoid re-creating the client for each request we do to the graph we are getting the following error and there is nothing we can do about it.

func (g *graph) getClient(tenantId uuid.UUID) (....) {
    cred, err := azidentity.NewClientSecretCredential(
        tenantId.String(),
        g.cfg.ClientID,
        g.cfg.ClientSecret,
        &azidentity.ClientSecretCredentialOptions{},
    )

    if err != nil {
        return nil, err
    }

    client, err = msgraphsdk.NewGraphServiceClientWithCredentials(cred, defaultScope)
    if err != nil {
        return nil, errors.Wrap(err, "failed to create graph client")
    }

    return client, nil
}

The error we are experiencing

The token used for this request is expected to be at least 45 minutes away from expiry. 
The token that was passed expires in 8 minutes and 23 seconds. Please use a fresher token.
rkodev commented 1 month ago

Hi @macnibblet , thanks for trying the SDK, I'd like to first understand why you want to cache the client and how long you intend to use the cached client? Best practice for security reasons requires tokens to expire and re-authentication to be done. I would suggest you create a refresh token function and call it when you get the error,

microsoft-github-policy-service[bot] commented 1 month ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.