1 year ago

#363319

test-img

Wet_Pantz

Calling microsoft graph api returns with 'InvalidAuthenticationToken' error

This is how I exchange the OBO token:

    public static async Task<string> ExchangeForOBOToken(ExchangeOBORequest request)
    {
        var contentData = new Dictionary<string, string>();
        contentData.Add("client_id", request.client_id);
        contentData.Add("client_secret", request.client_secret);
        contentData.Add("requested_token_use", request.requested_token_use);
        contentData.Add("grant_type", request.grant_type);
        contentData.Add("scope", request.scope);
        contentData.Add("assertion", request.assertion);

        using var httpClient = new HttpClient();
        using var content = new FormUrlEncodedContent(contentData);
        content.Headers.Clear();
        content.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

        HttpResponseMessage response = await httpClient.PostAsync("https://login.microsoftonline.com/common/oauth2/v2.0/token", content);

        var res=  await response.Content.ReadAsStringAsync();
        return res;
    }

And after that I get this response which is valid: enter image description here

After trying to call 'https://graph.microsoft.com/v1.0/me/' in postman with Bearer token set to the access token I got back from the OBO Exchange I get:

{
    "error": {
        "code": "InvalidAuthenticationToken",
        "message": "Access token validation failure. Invalid audience.",
        "innerError": {
            "date": "2022-04-01T11:19:36",
            "request-id": "0cbe8050-1113-4ea9-ab6c-41feafe08d73",
            "client-request-id": "0cbe8050-1113-4ea9-ab6c-41feafe08d73"
        }
    }
}

This is how I call it in postman:

enter image description here

microsoft-graph-api

microsoft-teams

microsoft-graph-teams

0 Answers

Your Answer

Accepted video resources