Skip to content

Commit

Permalink
updated readme with OAuth feature for public APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
sbansla committed Sep 23, 2024
1 parent 81d1758 commit 2bc465f
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 20 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ This library supports the following Java implementations:

For Java 7 support, use `twilio-java` major version `7.X.X`.

### Beta Annotation

To indicate that a class or method is in beta and subject to change, we use the `@Beta` annotation. For example:

```java

@Beta
public class ClassName {
// Class implementation
}


public class ClassName {
@Beta
public void init() {
// Implementation
}
}
```

## Installation

`twilio-java` uses Maven. At present the jars _are_ available from a public [maven](https://mvnrepository.com/artifact/com.twilio.sdk/twilio) repository.
Expand Down Expand Up @@ -194,6 +214,25 @@ public class Example {
}
```

### OAuth Feature
We are introducing Client Credentials Flow-based OAuth 2.0 authentication.
This feature is currently in beta and its implementation is subject to change.

```java
import com.twilio.credential.ClientCredentialProvider;

public class Test {
public static void main(String[] args) {
String clientId = "YOUR_CLIENT_ID";
String clientSecret = "YOUR_CLIENT_SECRET";

Twilio.init(new ClientCredentialProvider(clientId, clientSecret));

// Make API requests here using the authenticated client
}
}

```
### Iterate through records

The library automatically handles paging for you. With the `read` method, you can specify the number of records you want to receive (`limit`) and the maximum size you want each page fetch to be (`pageSize`). The library will then handle the task for you, fetching new pages under the hood as you iterate over the records.
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/twilio/Twilio.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.twilio;

import com.twilio.annotations.Preview;
import com.twilio.annotations.Beta;
import com.twilio.auth_strategy.AuthStrategy;
import com.twilio.constant.EnumConstants;
import com.twilio.credential.ClientCredentialProvider;
Expand Down Expand Up @@ -73,16 +73,17 @@ public static synchronized void init(final String username, final String passwor
Twilio.setAccountSid(null);
}

@Beta
public static synchronized void init(final CredentialProvider credentialProvider) {
Twilio.setCredentialProvider(credentialProvider);
}

@Beta
public static synchronized void init(final CredentialProvider credentialProvider, String accountSid) {
Twilio.setCredentialProvider(credentialProvider);
Twilio.setAccountSid(accountSid);
}

@Preview
private static void setCredentialProvider(final CredentialProvider credentialProvider) {
if (credentialProvider == null) {
throw new AuthenticationException("Credential Provider can not be null");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/twilio/TwilioNoAuth.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.twilio;

import com.twilio.annotations.Preview;
import com.twilio.annotations.Beta;
import com.twilio.http.noauth.NoAuthTwilioRestClient;
import lombok.Getter;

import java.util.List;
import com.twilio.exception.AuthenticationException;

@Preview
@Beta
public class TwilioNoAuth {
@Getter
private static List<String> userAgentExtensions;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/twilio/TwilioOrgsTokenAuth.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.twilio;

import com.twilio.annotations.Preview;
import com.twilio.annotations.Beta;
import com.twilio.exception.AuthenticationException;
import com.twilio.http.bearertoken.BearerTokenTwilioRestClient;
import lombok.Getter;
Expand All @@ -12,7 +12,7 @@
import com.twilio.http.bearertoken.TokenManager;
import com.twilio.http.bearertoken.OrgsTokenManager;

@Preview
@Beta
public class TwilioOrgsTokenAuth {
private static String accessToken;
@Getter
Expand Down
12 changes: 0 additions & 12 deletions src/main/java/com/twilio/annotations/Preview.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,10 @@ public void fetchToken() {
if (this.token == null || this.token.isEmpty() || isTokenExpired(this.token)) {
synchronized (TokenAuthStrategy.class){
if (this.token == null || this.token.isEmpty() || isTokenExpired(this.token)) {
this.token = tokenManager.fetchAccessToken();
this.token = tokenManager.fetchAccessToken(); // TODO: Exceptional handling
}
}
}
this.token = tokenManager.fetchAccessToken();
}

public boolean isTokenExpired(String token) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.twilio.credential;


import com.twilio.annotations.Beta;
import com.twilio.auth_strategy.AuthStrategy;
import com.twilio.auth_strategy.TokenAuthStrategy;
import com.twilio.constant.EnumConstants;
Expand All @@ -12,6 +13,7 @@
import lombok.Getter;
import lombok.Setter;

@Beta
public class ClientCredentialProvider extends CredentialProvider {
private String grantType;
private String clientId;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.twilio.http.bearertoken;

import com.twilio.annotations.Beta;
import com.twilio.exception.ApiException;
import com.twilio.rest.previewiam.v1.Token;
import com.twilio.rest.previewiam.v1.TokenCreator;

@Beta
public class ApiTokenManager implements TokenManager {

private final String grantType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.twilio.http.bearertoken;

import com.twilio.annotations.Beta;
import lombok.Setter;
import com.twilio.exception.ApiException;
import lombok.RequiredArgsConstructor;
import com.twilio.rest.previewiam.v1.Token;
import com.twilio.rest.previewiam.v1.TokenCreator;
@Beta
public class OrgsTokenManager implements TokenManager{

private final String grantType;
Expand Down

0 comments on commit 2bc465f

Please sign in to comment.