To demonstrate how to authenticate against the sipgate REST API using personal access tokens,
we query the /account endpoint which provides basic account information.
For further information regarding sipgate REST API please visit https://api.sipgate.com/v2/doc.
For more information on how to create a token, visit https://www.sipgate.io/rest-api/authentication#personalAccessToken.
- JDK 8
Navigate to the project's root directory.
Change token id and token in PersonalAccessToken.java:
String tokenId = "YOUR_SIPGATE_TOKEN_ID";
String token = "YOUR_SIPGATE_TOKEN";The token should have the account:read scope.
Run the application:
./gradlew runRequest parameters like url and credentials are defined as follows:
private static final String baseUrl = "https://api.sipgate.com/v2";
...
String credentials = tokenId + ":" + token;
String base64EncodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes());Note: Basic Auth requires the credentials to be Base64-encoded.
We use Unirest for request generation and execution.
The request URL consists of the base url defined above and the endpoint /account.
The value of the Authorization header consists of the keyword Basic followed by a space and the Base64-encoded credentials.
This example prints the HTTP status code and body of the response to the console.
HttpResponse<String> response = Unirest.get(baseUrl + "/account")
.header("Authorization", "Basic " + base64EncodedCredentials)
.asString();
System.out.println("Status: " + response.getStatus());
System.out.println("Body: " + response.getBody());If OAuth should be used for authorization instead of Basic Auth we do not use the
.basicAuth(tokenId, token)method. Instead we set theAuthorizationheader toBearerfollowed by a space and the access token:.header("Authorization", "Bearer " + accessToken). For an example application interacting with the sipgate API using OAuth see our sipgate.io Java Oauth example.
| reason | errorcode |
|---|---|
| token id and/or token are wrong | 401 |
| credentials not Base64-encoded | 401 |
| wrong REST API endpoint | 404 |
| wrong request method | 405 |
Please let us know how we can improve this example. If you have a specific feature request or found a bug, please use Issues or fork this repository and send a pull request with your improvements.
This project is licensed under The Unlicense (see LICENSE file).
This code uses the following external libraries
- unirest:
Licensed under the MIT License
Website: http://unirest.io/java
