KoenZomers / OneDriveAPI

API in .NET Framework 4.8.0, .NET Core 3.1 and .NET 6.0 to communicate with OneDrive Personal and OneDrive for Business
Eclipse Public License 1.0
110 stars 35 forks source link

GraphAPI GetAccessTokenFromRefreshToken missing parameter #18

Closed kennyvv closed 5 years ago

kennyvv commented 5 years ago

I was trying to use the GraphAPI and AuthenticateUsingRefreshToken, however i get the following exception.

KoenZomers.OneDrive.Api.Exceptions.TokenRetrievalFailedException: Failed to retrieve OneDrive access token. Additional information: AADSTS7000218: The request body must contain the following parameter: 'client_assertion' or 'client_secret'. Trace ID: ae843ab5-b298-4ff5-bd4e-7f52660a2300 Correlation ID: 8ce4b213-b349-47b2-8fc6-a21928a18c8b Timestamp: 2019-03-06 12:58:00Z at KoenZomers.OneDrive.Api.OneDriveApi.PostToTokenEndPoint(QueryStringBuilder queryBuilder) at KoenZomers.OneDrive.Api.OneDriveGraphApi.GetAccessTokenFromRefreshToken(String refreshToken, String[] scopes) at KoenZomers.OneDrive.Api.OneDriveGraphApi.GetAccessTokenFromRefreshToken(String refreshToken) at KoenZomers.OneDrive.Api.OneDriveApi.AuthenticateUsingRefreshToken(String refreshToken) --- End of inner exception stack trace ---

I thought i might have to set the client secret, however i can't set the property. What is the issue here?

KoenZomers commented 5 years ago

Sorry for the late follow up on this one. Can you try this again using the latest version? I remember fixing something which could be related to this around the time you reported it. If the issue still exists and you're still in need for a solution, it would help if you can share a Fiddler capture of the request being made via e-mail with me. Don't upload it here.

kgamecarter commented 5 years ago

When I use custom redirect url. Must provide client_secret in GetAccessTokenFromAuthorizationToken and GetAccessTokenFromRefreshToken.

public class MyOneDriveGraphApi : OneDriveGraphApi
{
    public MyOneDriveGraphApi(string applicationId, string clientSecret) : base(applicationId, clientSecret)
    {
        OneDriveApiBaseUrl = GraphApiBaseUrl + "me/";
    }

    protected override async Task<OneDriveAccessToken> GetAccessTokenFromAuthorizationToken(string authorizationToken)
    {
        var queryBuilder = new QueryStringBuilder();
        queryBuilder.Add("client_id", ClientId);
        queryBuilder.Add("scope", DefaultScopes.Aggregate((x, y) => $"{x} {y}"));
        queryBuilder.Add("code", authorizationToken);
        queryBuilder.Add("redirect_uri", AuthenticationRedirectUrl);
        queryBuilder.Add("grant_type", "authorization_code");
        queryBuilder.Add("client_secret", ClientSecret); // Add this
        return await PostToTokenEndPoint(queryBuilder);
    }

    protected override async Task<OneDriveAccessToken> GetAccessTokenFromRefreshToken(string refreshToken)
    {
        var queryBuilder = new QueryStringBuilder();
        queryBuilder.Add("client_id", ClientId);
        queryBuilder.Add("scope", DefaultScopes.Aggregate((x, y) => $"{x} {y}"));
        queryBuilder.Add("refresh_token", refreshToken);
        queryBuilder.Add("redirect_uri", AuthenticationRedirectUrl);
        queryBuilder.Add("grant_type", "refresh_token");
        queryBuilder.Add("client_secret", ClientSecret);
        return await PostToTokenEndPoint(queryBuilder);
    }
}
KoenZomers commented 5 years ago

Merged your PR to resolve this.