Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.SteadfastInnovation:box-android-sdk-v2:'
}
dependencies {
implementation("com.github.SteadfastInnovation:box-android-sdk-v2:")
}
<dependency>
<groupId>com.github.SteadfastInnovation</groupId>
<artifactId>box-android-sdk-v2</artifactId>
<version></version>
</dependency>
libraryDependencies += "com.github.SteadfastInnovation" % "box-android-sdk-v2" % ""
:dependencies [[com.github.SteadfastInnovation/box-android-sdk-v2 ""]]
The Android SDK depends on the Box Java SDK, so you must first import it into your workspace and make sure it builds. Import the Android SDK second and make the Java SDK a build dependency.
ant debug
.There is also experimental support for Gradle, allowing you to use the SDK with Android Studio. You must have Gradle 1.6 installed.
Before the Android SDK can be built, you must first install the Box Java SDK to your local Maven repository. This can be done by following the Gradle build instructions included in the Java SDK's readme.
The Android SDK also depends on the Android Support Library. Unfortunately, telling Gradle to look for the android-support JAR directly will likely result in dex merge conflicts if more than one project uses the support library. The easiest way to get around this is by also installing android-support-v4.jar to your local Maven repo. Run the following command, where $ANDROID_HOME points to your Android SDK root (you must have Maven installed).
mvn install:install-file \
-Dfile=$ANDROID_HOME/extras/android/support/v4/android-support-v4.jar \
-DgroupId=com.google.android \
-DartifactId=support-v4 \
-Dversion=r13 \
-Dpackaging=jar
You can now run gradle build
to build the SDK. However, building the library
by itself isn't very useful. To reference the SDK from another Android Gradle
project, add the following to your list of dependencies:
dependencies {
...
compile project(':box-android-sdk-private:BoxAndroidLibraryV2')
}
You can refer to the Android Gradle guide on multi project setups here .
Here is a more detailed tutorial on setting up box sdk using gradle.
For migration to V3 from earlier version, please see "Migrate to V3" section at the end.
Make sure you've set up your client id, client secret and (optional) redirect url correctly. Please refer to [developer document] (http://developers.box.com/oauth/) for more information. You can find a full example of how to perform authentication in the sample app.
The easiest way to authenticate is to use the OAuthActivity, which is included in the SDK. Add it to your manifest to use it.
// If you don't have a server redirect url, use this instead:
// Intent intent = createOAuthActivityIntent(context, clientId, clientSecret, false, "http://localhost");
Intent intent = OAuthActivity.createOAuthActivityIntent(this, clientId,
clientSecret);
startActivityForResult(intent);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_CANCELED) {
// Get the error message for why authentication failed.
String failMessage = data.getStringExtra(OAuthActivity.ERROR_MESSAGE);
// Implement your own logic to handle the error.
handleFail(failMessage);
} else {
// You will get an authenticated oath token object back upon success.
BoxAndroidOAuthData oauth = data.getParcelableExtra(OAuthActivity.BOX_CLIENT_OAUTH);
// If you don't want to supply a customized hub or parser, use null to fall back to defaults.
BoxAndroidClient client = new BoxAndroidClient(clientId, clientSecret, null, null, null);
client.authenticate(oauth);
youOwnMethod(client);
}
}
Our sdk auto refreshes OAuth access token when it expires. You will want to listen to the refresh events and update your stored token after refreshing.
boxClient.addOAuthRefreshListener(OAuthRefreshListener listener) {
new OAuthRefreshListener() {
@Override
public void onRefresh(IAuthData newAuthData) {
BoxOAuthToken oauthObject = boxClient.getAuthData();
// TODO: save the auth data.
}
}
}
Alternatively, you can use your own custom login activity with a WebView for authentication.
oauthView = (OAuthWebView) findViewById(R.id.oauthview);
oauthView.initializeAuthFlow(this, clientId, clientSecret);
boxClient.authenticate(oauthView, autoRefreshOAuth, getOAuthFlowListener());
// Create a listener listening to OAuth flow. The most important part you need
// to implement is onAuthFlowEvent and catch the OAUTH_CREATED event. This event
// indicates that the OAuth flow is done, the BoxClient is authenticated and
// that you can start making API calls.
private OAuthWebViewListener getOAuthFlowListener() {
return new OAuthWebViewListener() {
@Override
public onAuthFlowEvent(final IAuthEvent event,
final IAuthFlowMessage message) {
// Authentication is done, you can start using your BoxClient
// instance.
}
}
}
boxClient.addOAuthRefreshListener(OAuthRefreshListener listener) {
new OAuthRefreshListener() {
@Override
public void onRefresh(IAuthData newAuthData) {
BoxOAuthToken oauthObject = boxClient.getAuthData();
// TODO: save the auth data.
}
}
}
After you exit the app and return back, you can use the stored oauth data to authenticate:
// Re-authenticate using the previously obtained OAuth object.
boxClient.authenticate(oauthObject);
BoxFile boxFile = boxClient.getFilesManager().getFile(fileId, null);
Get default file info plus its description and SHA1.
BoxDefaultRequestObject requestObj = new BoxDefaultRequestObject();
requestObj.getRequestExtras().addField(BoxFile.FIELD_SHA1);
requestObj.getRequestExtras().addField(BoxFile.FIELD_DESCRIPTION);
BoxFile boxFile = boxClient.getFilesManager().getFile(fileId, requestObj);
Get 30 child items, starting from the 20th one, requiring etag, description, and name to be included.
BoxPagingRequestObject requestObj = BoxPagingRequestObject.BpagingRequestObject(30, 20);
requestObj.getRequestExtras().addField(BoxFolder.FIELD_NAME);
requestObj.getRequestExtras().addField(BoxFolder.FIELD_DESCRIPTION);
requestObj.getRequestExtras().addField(BoxFolder.FIELD_ETAG);
BoxCollection collection =
boxClient.getFoldersManager().getFolderItems(folderId, requestObj);
BoxFileUploadRequestObject requestObj =
BoxFileUploadRequestObject.uploadFileRequestObject(parent, "name", file);
BoxFile bFile = boxClient.getFilesManager().uploadFile(requestObj);
BoxFileUploadRequestObject requestObj =
BoxFileUploadRequestObject.uploadFileRequestObject(parent, "name", file)
.setListener(listener);
BoxAndroidFile bFile = boxClient.getFilesManager().uploadFile(requestObj);
boxClient.getFilesManager().downloadFile(fileId, null);
Delete a file, but only if the etag matches.
BoxDefaultRequestObject requestObj = new BoxDefaultRequestObject();
requestObject.getRequestExtras.setIfMatch(etag);
boxClient.getFilesManager().deleteFile(fileId, requestObj);
old code:
BoxFilesManager filesManager = boxClient.getFilesManager();
filesManager.doSomething(...);
new code:
IBoxFilesManager filesManager = boxClient.getFilesManager();
filesManager.doSomething(...);
old code:
BoxOAuthRequestObject obj = BoxOAuthRequestObject.crateOAuthRequestObject(code, clientId, clientSecret, redirectUrl);
BoxOAuthData oauth = oauthManager.createOAuth(obj);
new Code:
BoxOAuthData oauth = oauthManager.createOAuth(code, clientId, clientSecret, redirectUrl);
Old code:
Two ways to get it:
1. boxClient.getFilesManager.getFile(fileId, null);
2. boxClient.getFilesManager.getItem(fileId, null, BoxResourceType.FILE);
New code:
Two ways to get it:
1. same as old code.
2. boxClient.getBoxItemsManager.getItem(fileId, null, BoxResourceType.FILE);
old code:
boxClient.getFilesManager.getTrashFile(fileId, null);
new code:
boxClient.getTrashManager.getTrashFile(fileId, null);
Old code of create a shared link.
BoxFileRequestObject obj = BoxFileRequestObject. createSharedLinkRequestObject(......);
filesManager.createSharedLink(fileId, obj);
New code:
BoxSharedLinkRequestObject obj =
BoxSharedLinkRequestObject.
createSharedLinkRequestObject(sharedLinkEntity);
filesManager.createSharedLink(fileId, obj);
Also in order to provide cleaner interface, we moved the setters for basic http requests in the request objects to a "requestExtra".
Old code:
requestObject.addField("some field");
new code:
requestObject.getRequestExtras().addField("some field");
(Optional/Deprecated)
old code:
List<BoxFile> filesInCollection = BoxFilesManager.getFiles(collection);
new code:
List<BoxFile> filesInCollection = Utils.getTypedObjects(collection, BoxFile.class);
old code:
InputStream is = filesManager.downloadThumbnail(fileId, extension, null);
new code:
BoxThumbnail thumbnail = filesManager.downloadThumbnail(fileId, extension, null);
Copyright 2014 Box, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.