简体   繁体   中英

Adding an authentication token to header in Restlet

I am trying to make an API call using Restlet in java however when I run my code I get an org.restlet.resource.ResourceException: Unauthorized (401) - The request requires user authentication

The format for the API call is as follows for shell: curl "<api_url>" \ -H "Authorization: Bearer <api_token_here>"

However I am unsure how to add this authorization header in Restlet, as you are not able to add the header using .getRequest().getHeaders().add();

Additionally I have tried to set a challenge response however this also does not appear to work.

        API = new ClientResource(RequestURL);
        API.setProtocol(Protocol.HTTPS);

        ChallengeResponse AuthHeader = new ChallengeResponse(ChallengeScheme.CUSTOM);
        AuthHeader.setRawValue("Authorization: Bearer " + APIKey);

        API.getRequest().setChallengeResponse(AuthHeader);

        API.get();

I appear to have solved the issue with the following code:

    ChallengeResponse AuthHeader = new ChallengeResponse(ChallengeScheme.HTTP_OAUTH_BEARER); 
    AuthHeader.setRawValue(APIKey);
    AuthHeader.setIdentifier("Bearer");
    API.setChallengeResponse(AuthHeader);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM