Skip to content

Commit

Permalink
feat: Docs update to use custom clients when endpoints have no auth/b…
Browse files Browse the repository at this point in the history
…earer token auth and Support for PUT method (#802)

* Docs update for bearer token authentication and endpoints with no auth

* Support for put request in clients
  • Loading branch information
AsabuHere committed Jun 24, 2024
1 parent 6342d7f commit 6c9a757
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ public class Example {
}
}
```
### Initialize the client when endpoints does not use basic authentication
The above example shows how to initialize the client in case the endpoints use basic authentication. When the endpoint does not require any authentication, use TwilioNoAuth client instead.
There are endpoints like Organization domain which uses bearer token authentication. Custom Clients needs to be used in such cases and initialize them with the values required for access token generation.

To bypass the initialization step you can also use a custom token manager implementation. Token manager class should implement the Token interface and call a token generation endpoint of your choice.
Detailed examples [here](https://github.com/twilio/twilio-java/tree/main/examples)

### Environment Variables

Expand Down
9 changes: 8 additions & 1 deletion examples/BearerTokenAuthentication.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
class BearerTokenAuthenticationExamples {
public static void main {

private static final String GRANT_TYPE = "grant_type_to_be_used";
private static final String CLIENT_SID =
"client_id_of_the_organization";
private static final String CLIENT_SECRET = "client_secret_of_organization";
private static final String ORGANISATION_ID = "id_of_the_organization";

//Getting access token - Method #1
TwilioOrgsTokenAuth.init(GRANT_TYPE, CLIENT_ID, CLIENT_SECRET);

//Getting access token - Method #2
//To provide custom token manager implementation
//Need not call init method if customer token manager is passed
//TwilioOrgsTokenAuth.setTokenManager(new OrgsTokenManager(grantType, clientId, clientSecret));
//TwilioOrgsTokenAuth.setTokenManager(new CustomTokenManagerImpl(GRANT_TYPE, CLIENT_ID, CLIENT_SECRET));

fetchAccountDetails();
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/twilio/http/NetworkHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ public <T extends IRequest> Response makeRequest(final T request) {
}
}

if (method == HttpMethod.POST || method == HttpMethod.PUT) {
if (method != HttpMethod.GET) {
// TODO: It will be removed after one RC Release.
if (request.getContentType() == null) request.setContentType(EnumConstants.ContentType.FORM_URLENCODED);
if (EnumConstants.ContentType.JSON.getValue().equals(request.getContentType().getValue())) {
HttpEntity entity = new StringEntity(request.getBody(), ContentType.APPLICATION_JSON);
builder.setEntity(entity);
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/twilio/http/ValidationClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ public Response makeRequest(IRequest iRequest) {
}

HttpMethod method = request.getMethod();
if (method == HttpMethod.POST || method == HttpMethod.PUT) {
if (method != HttpMethod.GET) {
// TODO: It will be removed after one RC Release.
if (request.getContentType() == null) request.setContentType(EnumConstants.ContentType.FORM_URLENCODED);
if (EnumConstants.ContentType.JSON.getValue().equals(request.getContentType().getValue())) {
HttpEntity entity = new StringEntity(request.getBody(), ContentType.APPLICATION_JSON);
builder.setEntity(entity);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/twilio/http/noauth/NoAuthHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Response makeRequest(IRequest request) {
}
}

if (method == HttpMethod.POST) {
if (method != HttpMethod.GET) {
// TODO: It will be removed after one RC Release.
if (request.getContentType() == null) request.setContentType(EnumConstants.ContentType.FORM_URLENCODED);
if (EnumConstants.ContentType.JSON.getValue().equals(request.getContentType().getValue())) {
Expand Down

0 comments on commit 6c9a757

Please sign in to comment.