From f7a21678a797df6d0757a6a44781ad1dffa4005c Mon Sep 17 00:00:00 2001 From: Kyle Venn Date: Tue, 15 Mar 2016 18:34:26 -0400 Subject: [PATCH 1/5] builder can optionally take an access token; created helper within vimeoclient for saving accounts --- .../android/networking/example/TestApp.java | 35 ++++++++++++---- .../com/vimeo/networking/Configuration.java | 41 +++++++++++++++++-- .../com/vimeo/networking/VimeoClient.java | 29 +++++++++---- .../vimeo/networking/model/VimeoAccount.java | 22 +++------- 4 files changed, 90 insertions(+), 37 deletions(-) diff --git a/example/src/main/java/com/vimeo/android/networking/example/TestApp.java b/example/src/main/java/com/vimeo/android/networking/example/TestApp.java index b21f88406..b0e2dfa6b 100644 --- a/example/src/main/java/com/vimeo/android/networking/example/TestApp.java +++ b/example/src/main/java/com/vimeo/android/networking/example/TestApp.java @@ -21,6 +21,9 @@ public class TestApp extends Application { private static final String SCOPE = "private public create edit delete interact"; + private static final boolean IS_DEBUG_BUILD = false; + private static final boolean ACCESS_TOKEN_PROVIDED = true; + private static Context mContext; @Override @@ -28,10 +31,32 @@ public void onCreate() { super.onCreate(); mContext = this; - AccountPreferenceManager.initializeInstance(mContext); // + Configuration.Builder configBuilder; + // This check is just as for the example. In practice, you'd use one technique or the other. + if (ACCESS_TOKEN_PROVIDED) { + configBuilder = getAccessTokenBuilder(); + } else { + configBuilder = getIdAndSecretBuilder(); + } + if (IS_DEBUG_BUILD) { + // Disable cert pinning if debugging (so we can intercept packets) + configBuilder.enableCertPinning(false); + configBuilder.setLogLevel(LogLevel.VERBOSE); + } + VimeoClient.initialize(configBuilder.build()); + // + } + + public Configuration.Builder getAccessTokenBuilder() { + // The values file is left out of git, so you'll have to provide your own access token + String accessToken = getString(R.string.access_token); + return new Configuration.Builder(Vimeo.VIMEO_BASE_URL_STRING, accessToken); + } + + public Configuration.Builder getIdAndSecretBuilder() { // The values file is left out of git, so you'll have to provide your own id and secret String clientId = getString(R.string.client_id); String clientSecret = getString(R.string.client_secret); @@ -46,13 +71,7 @@ public void onCreate() { // Used for oauth flow .setCodeGrantRedirectUri(codeGrantRedirectUri); - if (/*isDebugBuild*/false) { - // Disable cert pinning if debugging (so we can intercept packets) - configBuilder.enableCertPinning(false); - configBuilder.setLogLevel(LogLevel.VERBOSE); - } - VimeoClient.initialize(configBuilder.build()); - // + return configBuilder; } public static Context getAppContext() { diff --git a/vimeo-networking/src/main/java/com/vimeo/networking/Configuration.java b/vimeo-networking/src/main/java/com/vimeo/networking/Configuration.java index 103f9db2b..70bc97d77 100644 --- a/vimeo-networking/src/main/java/com/vimeo/networking/Configuration.java +++ b/vimeo-networking/src/main/java/com/vimeo/networking/Configuration.java @@ -55,6 +55,8 @@ public class Configuration { public String clientSecret; public String scope; + public String accessToken; + @Nullable private AccountStore accountStore; public GsonDeserializer deserializer; @@ -100,6 +102,7 @@ public void deleteAccount(VimeoAccount account) { } } + @Nullable public VimeoAccount loadAccount() { if (accountStore == null) { return null; @@ -119,6 +122,9 @@ private Configuration(Builder builder) { this.clientID = builder.clientID; this.clientSecret = builder.clientSecret; this.scope = builder.scope; + + this.accessToken = builder.accessToken; + this.accountStore = builder.accountStore; this.deserializer = builder.deserializer; @@ -142,10 +148,12 @@ private Configuration(Builder builder) { } private boolean isValid() { - return (this.baseURLString != null && !this.baseURLString.trim().isEmpty() && - this.clientID != null && !this.clientID.trim().isEmpty() && - this.clientSecret != null && !this.clientSecret.trim().isEmpty() && - this.scope != null && !this.scope.trim().isEmpty()); + return this.baseURLString != null && (!this.baseURLString.trim().isEmpty() && + this.clientID != null && !this.clientID.trim().isEmpty() && + this.clientSecret != null && + !this.clientSecret.trim().isEmpty() && + this.scope != null && !this.scope.trim().isEmpty()) || + (this.accessToken != null && !this.accessToken.trim().isEmpty()); } /** @@ -157,6 +165,9 @@ public static class Builder { private String clientID; private String clientSecret; private String scope; + + private String accessToken; + private AccountStore accountStore; private GsonDeserializer deserializer = new GsonDeserializer(); @@ -174,6 +185,21 @@ public static class Builder { public DebugLoggerInterface debugLogger = new DebugLogger(); public LogLevel logLevel = LogLevel.DEBUG; + /** + * The most basic builder constructor. If you've only provided an access token, you'll only be able to + * make requests for the given access token's account. + *

+ * If you'd like the ability to switch accounts, you'll have to set a client id and client secret. + * If you'd like the ability to persist accounts, you'll have to set an account store + * + * @param baseURLString The base url pointing to the Vimeo api. Something like: {@link Vimeo#VIMEO_BASE_URL_STRING} + * @param accessToken Token provided by the Vimeo developer console + */ + public Builder(String baseURLString, String accessToken) { + this.baseURLString = baseURLString; + this.accessToken = accessToken; + } + public Builder(String baseURLString, String clientID, String clientSecret, String scope) { this(baseURLString, clientID, clientSecret, scope, null, null); } @@ -202,6 +228,13 @@ public Builder(String baseURLString, String clientId, String clientSecret, Strin this.deserializer = deserializer; } + /** If you used the basic Builder access token constructor but have the intent of */ + public Builder setClientIdAndSecret(String clientId, String clientSecret) { + this.clientID = clientId; + this.clientSecret = clientSecret; + return this; + } + public Builder setAccountStore(AccountStore accountStore) { this.accountStore = accountStore; return this; diff --git a/vimeo-networking/src/main/java/com/vimeo/networking/VimeoClient.java b/vimeo-networking/src/main/java/com/vimeo/networking/VimeoClient.java index c98dd25c8..07a614a7e 100644 --- a/vimeo-networking/src/main/java/com/vimeo/networking/VimeoClient.java +++ b/vimeo-networking/src/main/java/com/vimeo/networking/VimeoClient.java @@ -66,6 +66,7 @@ */ public class VimeoClient { + @NotNull private Configuration configuration; private VimeoService vimeoService; @Nullable @@ -195,12 +196,19 @@ public VimeoAccount getVimeoAccount() { public void setVimeoAccount(@Nullable VimeoAccount vimeoAccount) { if (vimeoAccount == null) { - vimeoAccount = new VimeoAccount(); + vimeoAccount = new VimeoAccount(this.configuration.accessToken); + this.configuration.saveAccount(vimeoAccount, null, null); } this.vimeoAccount = vimeoAccount; } + /** Sets the {@link #vimeoAccount} field as well as triggering the saveAccount event for the account store */ + public void saveAccount(@Nullable VimeoAccount vimeoAccount, String email, String password) { + setVimeoAccount(vimeoAccount); + this.configuration.saveAccount(vimeoAccount, email, password); + } + public Configuration getConfiguration() { return this.configuration; } @@ -451,9 +459,7 @@ public VimeoAccount logIn(String email, String password) { e.printStackTrace(); } - this.setVimeoAccount(vimeoAccount); - - this.configuration.saveAccount(vimeoAccount, email, password); + saveAccount(vimeoAccount, email, password); return vimeoAccount; } @@ -491,7 +497,15 @@ public Call loginWithFacebookToken(String facebookToken, String em * @param callback Callback for handling logout */ public Call logOut(@Nullable final VimeoCallback callback) { - + // If you've provided an access token to the configuration builder, we're assuming that you wouldn't + // want to be able to log out of it, because this would invalidate the constant you've provided us. + if (configuration.accessToken.equals(vimeoAccount.getAccessToken())) { + if (callback != null) { + callback.failure(new VimeoError( + "You can't log out of the account provided through the configuration builder")); + } + return null; + } Call call = this.vimeoService.logOut(getAuthHeader()); if (callback != null) { callback.setCall(call); @@ -562,15 +576,14 @@ public AccountCallback(VimeoClient client, String email, String password, AuthCa @Override public void success(VimeoAccount vimeoAccount) { - this.client.setVimeoAccount(vimeoAccount); if (vimeoAccount.getUser() != null && (this.email == null || this.email.isEmpty())) { // We must always have a `name` field, which is used by the Android Account Manager for // display in the device Settings -> Accounts [KZ] 12/17/15 String name = (vimeoAccount.getUser().name != null) ? vimeoAccount.getUser().name : vimeoAccount.getUser().uri; - this.client.configuration.saveAccount(vimeoAccount, name, null); + this.client.saveAccount(vimeoAccount, name, null); } else { - this.client.configuration.saveAccount(vimeoAccount, this.email, this.password); + this.client.saveAccount(vimeoAccount, this.email, this.password); } this.callback.success(); } diff --git a/vimeo-networking/src/main/java/com/vimeo/networking/model/VimeoAccount.java b/vimeo-networking/src/main/java/com/vimeo/networking/model/VimeoAccount.java index 9c2d4fc22..ada4953b3 100644 --- a/vimeo-networking/src/main/java/com/vimeo/networking/model/VimeoAccount.java +++ b/vimeo-networking/src/main/java/com/vimeo/networking/model/VimeoAccount.java @@ -26,6 +26,8 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import org.jetbrains.annotations.Nullable; + import java.io.Serializable; /** @@ -45,24 +47,8 @@ public class VimeoAccount implements Serializable { private User user; private String userJSON; - public VimeoAccount() { - - } - - public VimeoAccount(String accessToken, String tokenType, String scope, String userJSON) { - if (accessToken == null || accessToken.isEmpty() || tokenType == null || - tokenType.isEmpty() || scope == null || scope.isEmpty()) { - throw new AssertionError("Account can only be created with token, tokenType, scope"); - } - + public VimeoAccount(@Nullable String accessToken) { this.accessToken = accessToken; - this.tokenType = tokenType; - this.scope = scope; - - Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) - .create(); - - this.user = gson.fromJson(userJSON, User.class); } public boolean isAuthenticated() { @@ -81,6 +67,7 @@ public String getScope() { return this.scope; } + @Nullable public User getUser() { return this.user; } @@ -89,6 +76,7 @@ public void setUser(User user) { this.user = user; } + @Nullable public String getUserJSON() // For AccountManager.userData [AH] { if (this.user == null) { From add95d802f42c7928a42c96284dbb007ee64c4b0 Mon Sep 17 00:00:00 2001 From: Kyle Venn Date: Tue, 15 Mar 2016 19:04:23 -0400 Subject: [PATCH 2/5] fixing npe on logout; re-adding valuable constructor --- README.md | 19 +++++++++++++++++++ .../com/vimeo/networking/VimeoClient.java | 3 ++- .../vimeo/networking/model/VimeoAccount.java | 16 ++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6e2cf89f8..13efcfae8 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,11 @@ compile project(':vimeo-networking-java:vimeo-networking') ``` ### Initialization + +The `VimeoClient` instance is highly customizable through the use of the `Configuration.Builder` class. There are a few different default `Builder` constructors that cover core functionality. +In the below sections, we cover examples of ways to customize your `VimeoClient` instance. Full implementations are in the example app. + +#### Configuration Builder for Apps with Account Management On app launch, configure `VimeoClient` with your client key, secret, and scope strings. And once initialization is complete, authenticate if necessary. This authentication can include [client credentials](#client-credentials-grant) or authentication via [code grant](#oauth-authorization-code-grant). If you choose to pass in an `AccountStore`, the authenticated account will automatically be persisted. Once authentication is complete, you can access the persisted `VimeoAccount` object through `VimeoClient#getVimeoAccount()`. For a basic implementation of an `AccountStore`, please refer to [this file in the example app](example/src/main/java/com/vimeo/android/networking/example/vimeonetworking/TestAccountStore.java) @@ -71,6 +76,20 @@ Setting the cache directory and deserializer are optional but highly recommended * The deserializer allows for deserialization on a background thread. Without it, object deserialization will occur on the main (UI) thread. This can be bad for performance if the API response bodies are large. * The cache directory is to allow caching of requests. Without it, no requests will be cached and all other cache settings will be ignored. +#### Configuration Builder for Apps with Only One Developer Account +You can skip the need for [client credentials](#client-credentials-grant) or [code grant](#oauth-authorization-code-grant) requests if you provide an access token up front. With this access token you'll be able to make any requests that the access token's scope allows. + +You can generate an access token in the Authentication tab once you select your app from the [list here](https://developer.vimeo.com/apps). + +Once you have the access token, you can easily initialize your `VimeoClient` instance with the below builder. + +```java +String accessToken = getString(R.string.access_token); +return new Configuration.Builder(Vimeo.VIMEO_BASE_URL_STRING, accessToken); +``` + +*Note: You will not be able to log out of the account associated with the access token provided to the `Configuration.Builder` + ### Authentication All calls to the Vimeo API must be [authenticated](https://developer.vimeo.com/api/authentication). This means that before making requests to the API you must authenticate and obtain an access token. Two authentication methods are provided: diff --git a/vimeo-networking/src/main/java/com/vimeo/networking/VimeoClient.java b/vimeo-networking/src/main/java/com/vimeo/networking/VimeoClient.java index 07a614a7e..0c4057e32 100644 --- a/vimeo-networking/src/main/java/com/vimeo/networking/VimeoClient.java +++ b/vimeo-networking/src/main/java/com/vimeo/networking/VimeoClient.java @@ -499,7 +499,8 @@ public Call loginWithFacebookToken(String facebookToken, String em public Call logOut(@Nullable final VimeoCallback callback) { // If you've provided an access token to the configuration builder, we're assuming that you wouldn't // want to be able to log out of it, because this would invalidate the constant you've provided us. - if (configuration.accessToken.equals(vimeoAccount.getAccessToken())) { + if (configuration.accessToken != null && + configuration.accessToken.equals(vimeoAccount.getAccessToken())) { if (callback != null) { callback.failure(new VimeoError( "You can't log out of the account provided through the configuration builder")); diff --git a/vimeo-networking/src/main/java/com/vimeo/networking/model/VimeoAccount.java b/vimeo-networking/src/main/java/com/vimeo/networking/model/VimeoAccount.java index ada4953b3..f47e2da7d 100644 --- a/vimeo-networking/src/main/java/com/vimeo/networking/model/VimeoAccount.java +++ b/vimeo-networking/src/main/java/com/vimeo/networking/model/VimeoAccount.java @@ -51,6 +51,22 @@ public VimeoAccount(@Nullable String accessToken) { this.accessToken = accessToken; } + public VimeoAccount(String accessToken, String tokenType, String scope, String userJSON) { + if (accessToken == null || accessToken.isEmpty() || tokenType == null || + tokenType.isEmpty() || scope == null || scope.isEmpty()) { + throw new AssertionError("Account can only be created with token, tokenType, scope"); + } + + this.accessToken = accessToken; + this.tokenType = tokenType; + this.scope = scope; + + Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) + .create(); + + this.user = gson.fromJson(userJSON, User.class); + } + public boolean isAuthenticated() { return (this.accessToken != null && !this.accessToken.isEmpty()); } From e9ca077158bef5ba8941cca099633c2f545cbb7e Mon Sep 17 00:00:00 2001 From: Kyle Venn Date: Tue, 15 Mar 2016 19:08:55 -0400 Subject: [PATCH 3/5] adding clarifying comment for user fetching --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 13efcfae8..103939a02 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,8 @@ String accessToken = getString(R.string.access_token); return new Configuration.Builder(Vimeo.VIMEO_BASE_URL_STRING, accessToken); ``` +After providing the access token, if you'd like to have access to the associated `User` object you'll need to make a call to `VimeoClient#fetchCurrentUser`. If you're using an account store, you can update the `VimeoAccount` with the new `User` object. + *Note: You will not be able to log out of the account associated with the access token provided to the `Configuration.Builder` ### Authentication From eb5d5a94115e7bee4a46fa89a7129b8e15db0bb8 Mon Sep 17 00:00:00 2001 From: Kyle Venn Date: Tue, 15 Mar 2016 19:14:29 -0400 Subject: [PATCH 4/5] added clarifying comment to example app --- .../java/com/vimeo/android/networking/example/TestApp.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/example/src/main/java/com/vimeo/android/networking/example/TestApp.java b/example/src/main/java/com/vimeo/android/networking/example/TestApp.java index b0e2dfa6b..a75aaf816 100644 --- a/example/src/main/java/com/vimeo/android/networking/example/TestApp.java +++ b/example/src/main/java/com/vimeo/android/networking/example/TestApp.java @@ -22,7 +22,8 @@ public class TestApp extends Application { private static final String SCOPE = "private public create edit delete interact"; private static final boolean IS_DEBUG_BUILD = false; - private static final boolean ACCESS_TOKEN_PROVIDED = true; + // Switch to true to see how access token auth works. + private static final boolean ACCESS_TOKEN_PROVIDED = false; private static Context mContext; From 50893166b8e95a0a2f1f3d4a89fd7ca005b49dbd Mon Sep 17 00:00:00 2001 From: Kyle Venn Date: Wed, 16 Mar 2016 15:22:48 -0400 Subject: [PATCH 5/5] respoding to PR comments; removing the need for a base url; added setter for access token; added clarifying comments --- README.md | 11 +++--- .../android/networking/example/TestApp.java | 11 +++--- .../com/vimeo/networking/Configuration.java | 38 ++++++++++++++----- .../com/vimeo/networking/VimeoClient.java | 8 ++-- 4 files changed, 45 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 103939a02..ec77ce2f0 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,6 @@ On app launch, configure `VimeoClient` with your client key, secret, and scope s If you choose to pass in an `AccountStore`, the authenticated account will automatically be persisted. Once authentication is complete, you can access the persisted `VimeoAccount` object through `VimeoClient#getVimeoAccount()`. For a basic implementation of an `AccountStore`, please refer to [this file in the example app](example/src/main/java/com/vimeo/android/networking/example/vimeonetworking/TestAccountStore.java) ```java /** - * @param baseURLString The base url pointing to the Vimeo api. Something like: {@link Vimeo#VIMEO_BASE_URL_STRING} * @param clientId The client id provided to you from the developer console * @param clientSecret The client secret provided to you from the developer console * @param scope Space separated list of scopes @@ -68,7 +67,7 @@ If you choose to pass in an `AccountStore`, the authenticated account will autom * @param deserializer (Optional, Recommended) Extend GsonDeserializer to allow for deserialization on a background thread */ Configuration.Builder configBuilder = - new Configuration.Builder(Vimeo.VIMEO_BASE_URL_STRING, clientId, clientSecret, SCOPE, + new Configuration.Builder(clientId, clientSecret, SCOPE, testAccountStore, new AndroidGsonDeserializer()) .setCacheDirectory(this.getCacheDir()) ``` @@ -78,6 +77,7 @@ Setting the cache directory and deserializer are optional but highly recommended #### Configuration Builder for Apps with Only One Developer Account You can skip the need for [client credentials](#client-credentials-grant) or [code grant](#oauth-authorization-code-grant) requests if you provide an access token up front. With this access token you'll be able to make any requests that the access token's scope allows. +You will NOT be able to switch accounts if you only supply the access token. To do that, refer to the above section. You can generate an access token in the Authentication tab once you select your app from the [list here](https://developer.vimeo.com/apps). @@ -85,12 +85,13 @@ Once you have the access token, you can easily initialize your `VimeoClient` ins ```java String accessToken = getString(R.string.access_token); -return new Configuration.Builder(Vimeo.VIMEO_BASE_URL_STRING, accessToken); +return new Configuration.Builder(accessToken); ``` After providing the access token, if you'd like to have access to the associated `User` object you'll need to make a call to `VimeoClient#fetchCurrentUser`. If you're using an account store, you can update the `VimeoAccount` with the new `User` object. -*Note: You will not be able to log out of the account associated with the access token provided to the `Configuration.Builder` +*Note: You will not be able to log out of the account associated with the access token provided to the `Configuration.Builder`. This is because we wouldn't want anyone to accidentally invalidate/delete the token which is being used to authenticate users in a production application. You will still be able to delete the token via the web [developer console](https://developer.vimeo.com/apps/). +If this seems like restricting functionality, please log an issue to the [issue tracker](https://github.com/vimeo/vimeo-networking-java/issues). ### Authentication All calls to the Vimeo API must be [authenticated](https://developer.vimeo.com/api/authentication). This means that before making requests to the API you must authenticate and obtain an access token. Two authentication methods are provided: @@ -102,7 +103,7 @@ All calls to the Vimeo API must be [authenticated](https://developer.vimeo.com/a #### Client Credentials Grant ```java // You can't make any requests to the api without an access token. This will get you a basic -// "Client Credentials" grant which will allow you to make requests +// "Client Credentials" grant which will allow you to make requests. This requires a client id and client secret. private void authenticateWithClientCredentials() { VimeoClient.getInstance().authorizeWithClientCredentialsGrant(new AuthCallback() { @Override diff --git a/example/src/main/java/com/vimeo/android/networking/example/TestApp.java b/example/src/main/java/com/vimeo/android/networking/example/TestApp.java index a75aaf816..c623a925c 100644 --- a/example/src/main/java/com/vimeo/android/networking/example/TestApp.java +++ b/example/src/main/java/com/vimeo/android/networking/example/TestApp.java @@ -10,7 +10,6 @@ import com.vimeo.android.networking.example.vimeonetworking.NetworkingLogger; import com.vimeo.android.networking.example.vimeonetworking.TestAccountStore; import com.vimeo.networking.Configuration; -import com.vimeo.networking.Vimeo; import com.vimeo.networking.Vimeo.LogLevel; import com.vimeo.networking.VimeoClient; @@ -40,7 +39,7 @@ public void onCreate() { if (ACCESS_TOKEN_PROVIDED) { configBuilder = getAccessTokenBuilder(); } else { - configBuilder = getIdAndSecretBuilder(); + configBuilder = getClientIdAndClientSecretBuilder(); } if (IS_DEBUG_BUILD) { // Disable cert pinning if debugging (so we can intercept packets) @@ -54,10 +53,10 @@ public void onCreate() { public Configuration.Builder getAccessTokenBuilder() { // The values file is left out of git, so you'll have to provide your own access token String accessToken = getString(R.string.access_token); - return new Configuration.Builder(Vimeo.VIMEO_BASE_URL_STRING, accessToken); + return new Configuration.Builder(accessToken); } - public Configuration.Builder getIdAndSecretBuilder() { + public Configuration.Builder getClientIdAndClientSecretBuilder() { // The values file is left out of git, so you'll have to provide your own id and secret String clientId = getString(R.string.client_id); String clientSecret = getString(R.string.client_secret); @@ -65,8 +64,8 @@ public Configuration.Builder getIdAndSecretBuilder() { getString(R.string.deeplink_redirect_host); TestAccountStore testAccountStore = new TestAccountStore(this.getApplicationContext()); Configuration.Builder configBuilder = - new Configuration.Builder(Vimeo.VIMEO_BASE_URL_STRING, clientId, clientSecret, SCOPE, - testAccountStore, new AndroidGsonDeserializer()); + new Configuration.Builder(clientId, clientSecret, SCOPE, testAccountStore, + new AndroidGsonDeserializer()); configBuilder.setCacheDirectory(this.getCacheDir()) .setUserAgentString(getUserAgentString(this)).setDebugLogger(new NetworkingLogger()) // Used for oauth flow diff --git a/vimeo-networking/src/main/java/com/vimeo/networking/Configuration.java b/vimeo-networking/src/main/java/com/vimeo/networking/Configuration.java index 70bc97d77..34c7a143e 100644 --- a/vimeo-networking/src/main/java/com/vimeo/networking/Configuration.java +++ b/vimeo-networking/src/main/java/com/vimeo/networking/Configuration.java @@ -161,7 +161,7 @@ private boolean isValid() { */ public static class Builder { - private String baseURLString; + private String baseURLString = Vimeo.VIMEO_BASE_URL_STRING; private String clientID; private String clientSecret; private String scope; @@ -189,21 +189,30 @@ public static class Builder { * The most basic builder constructor. If you've only provided an access token, you'll only be able to * make requests for the given access token's account. *

- * If you'd like the ability to switch accounts, you'll have to set a client id and client secret. - * If you'd like the ability to persist accounts, you'll have to set an account store + * If you'd like the ability to switch accounts or request a client credentials grant, you'll have to set a client id and client secret. + * If you'd like the ability to persist accounts, you'll have to set an account store. + * If you'd like the ability to issue code grant, you'll have to set a code grant redirect uri. * - * @param baseURLString The base url pointing to the Vimeo api. Something like: {@link Vimeo#VIMEO_BASE_URL_STRING} - * @param accessToken Token provided by the Vimeo developer console + * @param accessToken Token provided by the Vimeo developer console */ - public Builder(String baseURLString, String accessToken) { - this.baseURLString = baseURLString; + public Builder(String accessToken) { this.accessToken = accessToken; } + public Builder(String clientID, String clientSecret, String scope) { + this(null, clientID, clientSecret, scope, null, null); + } + + @Deprecated public Builder(String baseURLString, String clientID, String clientSecret, String scope) { this(baseURLString, clientID, clientSecret, scope, null, null); } + public Builder(String clientId, String clientSecret, String scope, + @Nullable AccountStore accountStore, @Nullable GsonDeserializer deserializer) { + this(null, clientId, clientSecret, scope, accountStore, deserializer); + } + /** * The constructor for the Configuration Builder. Only the last two arguments are optional but it is * highly recommended that you pass in a deserializer since, without one, deserialization will occur @@ -218,9 +227,10 @@ public Builder(String baseURLString, String clientID, String clientSecret, Strin * @param accountStore (Optional, Recommended) An implementation that can be used to interface with Androids Account Manager * @param deserializer (Optional, Recommended) Extend GsonDeserializer to allow for deserialization on a background thread */ - public Builder(String baseURLString, String clientId, String clientSecret, String scope, + @Deprecated + public Builder(@Nullable String baseURLString, String clientId, String clientSecret, String scope, @Nullable AccountStore accountStore, @Nullable GsonDeserializer deserializer) { - this.baseURLString = baseURLString; + this.baseURLString = baseURLString == null ? this.baseURLString : baseURLString; this.clientID = clientId; this.clientSecret = clientSecret; this.scope = scope; @@ -228,6 +238,11 @@ public Builder(String baseURLString, String clientId, String clientSecret, Strin this.deserializer = deserializer; } + public Builder setBaseUrl(String baseUrl) { + this.baseURLString = baseUrl; + return this; + } + /** If you used the basic Builder access token constructor but have the intent of */ public Builder setClientIdAndSecret(String clientId, String clientSecret) { this.clientID = clientId; @@ -245,6 +260,11 @@ public Builder setGsonDeserializer(GsonDeserializer deserializer) { return this; } + public Builder setAccessToken(String accessToken) { + this.accessToken = accessToken; + return this; + } + public Builder setCodeGrantRedirectUri(String redirectUri) { this.codeGrantRedirectUri = redirectUri; return this; diff --git a/vimeo-networking/src/main/java/com/vimeo/networking/VimeoClient.java b/vimeo-networking/src/main/java/com/vimeo/networking/VimeoClient.java index 0c4057e32..731a09119 100644 --- a/vimeo-networking/src/main/java/com/vimeo/networking/VimeoClient.java +++ b/vimeo-networking/src/main/java/com/vimeo/networking/VimeoClient.java @@ -297,9 +297,10 @@ public Call authenticateWithCodeGrant(String uri, AuthCallback cal } /** - * Authorizes users of the app who are not signed in. + * Authorizes users of the app who are not signed in. This call requires a client id and client secret to be + * set on the initial Configuration. *

- * Leaves User as null in {@link VimeoAccount} model and populates the rest + * Leaves User as null in {@link VimeoAccount} model and populates the rest. * * @param callback Callback pertaining to authentication */ @@ -503,7 +504,8 @@ public Call logOut(@Nullable final VimeoCallback callback) { configuration.accessToken.equals(vimeoAccount.getAccessToken())) { if (callback != null) { callback.failure(new VimeoError( - "You can't log out of the account provided through the configuration builder")); + "You can't log out of the account provided through the configuration builder. " + + "This is to ensure the access token generated in the developer console isn't accidentally invalidated. ")); } return null; }