com.microsoft.rest.credentials.TokenCredentials Java Examples

The following examples show how to use com.microsoft.rest.credentials.TokenCredentials. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CredentialsTests.java    From autorest-clientruntime-for-java with MIT License 6 votes vote down vote up
@Test
public void tokenCredentialsTest() throws Exception {
    TokenCredentials credentials = new TokenCredentials(null, "this_is_a_token");
    OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder();
    credentials.applyCredentialsFilter(clientBuilder);
    clientBuilder.addInterceptor(
            new Interceptor() {
                @Override
                public Response intercept(Chain chain) throws IOException {
                    String header = chain.request().header("Authorization");
                    Assert.assertEquals("Bearer this_is_a_token", header);
                    return new Response.Builder()
                            .request(chain.request())
                            .code(200)
                            .message("OK")
                            .protocol(Protocol.HTTP_1_1)
                            .body(ResponseBody.create(MediaType.parse("text/plain"), "azure rocks"))
                            .build();
                }
            });
    ServiceClient serviceClient = new ServiceClient("http://localhost", clientBuilder, new Retrofit.Builder()) { };
    Response response = serviceClient.httpClient().newCall(new Request.Builder().url("http://localhost").build()).execute();
    Assert.assertEquals(200, response.code());
}
 
Example #2
Source File: TokenBatchCredentialProvider.java    From reef with Apache License 2.0 6 votes vote down vote up
/**
 * Returns credentials for Azure Batch account.
 * @return an implementation of {@link BatchCredentials} which is based on the token provided by Azure Batch.
 */
@Override
public BatchCredentials getCredentials() {

  final TokenCredentials tokenCredentials = new TokenCredentials(null, System.getenv(AZ_BATCH_AUTH_TOKEN_ENV));

  return new BatchCredentials() {
    @Override
    public String baseUrl() {
      return azureBatchAccountUri;
    }

    @Override
    public void applyCredentialsFilter(final OkHttpClient.Builder builder) {
      tokenCredentials.applyCredentialsFilter(builder);
    }
  };
}