-
Notifications
You must be signed in to change notification settings - Fork 449
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-Enable Examples running during PRs (#460)
* Re Enable Examples * Ensure Tests are always run because they are non deterministic.
- Loading branch information
1 parent
dc69c9f
commit 460d63b
Showing
38 changed files
with
413 additions
and
363 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,32 @@ | ||
apply plugin: 'java' | ||
apply plugin: 'kotlin' | ||
apply from: '../../gradle/integration-test-config.gradle' | ||
plugins { | ||
id("java") | ||
id("kotlin") | ||
} | ||
|
||
description = 'Consolidated Examples' | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_11 | ||
targetCompatibility = JavaVersion.VERSION_11 | ||
} | ||
|
||
dependencies { | ||
testImplementation 'org.eclipse.jetty.aggregate:jetty-server:8.1.18.v20150929' | ||
testImplementation 'javax.servlet:javax.servlet-api:3.1.0' | ||
testImplementation 'org.apache.commons:commons-lang3:3.4' | ||
testImplementation 'com.fasterxml.jackson.core:jackson-core:2.7.9' | ||
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.7.4' | ||
implementation(project(":dropbox-sdk-java")) | ||
implementation("org.eclipse.jetty.aggregate:jetty-server:8.1.18.v20150929") | ||
implementation("javax.servlet:javax.servlet-api:3.1.0") | ||
implementation("org.apache.commons:commons-lang3:3.4") | ||
implementation("com.fasterxml.jackson.core:jackson-core:2.7.9") | ||
implementation("com.fasterxml.jackson.core:jackson-databind:2.7.4") | ||
implementation(dropboxJavaSdkLibs.okhttp3) | ||
implementation("org.json:json:20220320") | ||
|
||
testImplementation(dropboxJavaSdkLibs.test.junit) | ||
testImplementation(dropboxJavaSdkLibs.okhttp3) | ||
testImplementation("org.json:json:20220320") | ||
} | ||
|
||
test { | ||
// Uses the "ci" gradle property and sets it as an environment variable for the test | ||
environment("ci", project.findProperty("ci")) | ||
|
||
// Ensure that tests are always run because integration tests are non-deterministic | ||
outputs.upToDateWhen { false } | ||
} |
75 changes: 75 additions & 0 deletions
75
examples/examples/src/main/java/com/dropbox/core/examples/CredentialsUtil.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.dropbox.core.examples | ||
|
||
import com.dropbox.core.DbxAuthInfo | ||
import com.dropbox.core.DbxHost | ||
import com.dropbox.core.oauth.DbxCredential | ||
import okhttp3.HttpUrl.Companion.toHttpUrl | ||
import okhttp3.OkHttpClient | ||
import okhttp3.Request | ||
import okhttp3.RequestBody.Companion.toRequestBody | ||
import org.json.JSONObject | ||
import java.io.File | ||
|
||
object CredentialsUtil { | ||
|
||
private val okhttp = OkHttpClient.Builder().build() | ||
|
||
data class OAuth2TokenInputs( | ||
val appKey: String?, | ||
val appSecret: String?, | ||
val refreshToken: String?, | ||
) { | ||
companion object { | ||
fun fromEnvironment(): OAuth2TokenInputs { | ||
val appKey = System.getenv("APP_KEY") | ||
val appSecret = System.getenv("APP_SECRET") | ||
val refreshToken = System.getenv("REFRESH_TOKEN") | ||
|
||
require(appKey?.isNotEmpty() == true) { "APP_KEY is not set as an environment variable." } | ||
require(appSecret?.isNotEmpty() == true) { "APP_SECRET is not set as an environment variable." } | ||
require(refreshToken?.isNotEmpty() == true) { "REFRESH_TOKEN is not set as an environment variable." } | ||
|
||
return OAuth2TokenInputs( | ||
appKey = appKey, | ||
appSecret = appSecret, | ||
refreshToken = refreshToken | ||
) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Will call the token API and get a new Access Token in exchange for a Refresh Token and App Key | ||
*/ | ||
private fun credsFromRefreshToken(refreshToken: String, appKey: String): DbxCredential { | ||
val request = Request.Builder() | ||
.url( | ||
"https://api.dropbox.com/oauth2/token" | ||
.toHttpUrl() | ||
.newBuilder() | ||
.apply { | ||
addQueryParameter("grant_type", "refresh_token") | ||
addQueryParameter("refresh_token", refreshToken) | ||
addQueryParameter("client_id", appKey) | ||
}.build() | ||
) | ||
.post("".toRequestBody()) | ||
.build() | ||
|
||
val response = okhttp.newCall(request).execute() | ||
val jsonObj = JSONObject(response.body?.string()) | ||
val accessToken = jsonObj.getString("access_token") | ||
return DbxCredential(accessToken) | ||
} | ||
|
||
private val authOutputFile = File("../../auth_output") | ||
|
||
fun getDbxCredential(): DbxCredential { | ||
return DbxCredential.Reader.readFromFile(authOutputFile) | ||
} | ||
|
||
fun getAuthInfo(): DbxAuthInfo { | ||
return DbxAuthInfo.Reader.readFromFile(authOutputFile) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
examples/examples/src/main/java/com/dropbox/core/examples/authorize/AuthorizeExample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.dropbox.core.examples.authorize | ||
|
||
import com.dropbox.core.DbxAppInfo | ||
import com.dropbox.core.DbxAuthFinish | ||
import com.dropbox.core.json.JsonReader | ||
import com.dropbox.core.oauth.DbxCredential | ||
import java.io.File | ||
import java.io.IOException | ||
import java.util.logging.Level | ||
import java.util.logging.Logger | ||
|
||
/** | ||
* | ||
* An example command-line application that runs through the web-based OAuth | ||
* flow (using [DbxWebAuth]). It grabs short-live token as well as | ||
* refresh token from server. It stores all authentication related data into the new | ||
* DbxCredential object and save them to file. | ||
*/ | ||
object AuthorizeExample { | ||
@JvmStatic | ||
fun main(args: Array<String>) { | ||
// Only display important log messages. | ||
Logger.getLogger("").level = Level.WARNING | ||
if (args.size != 3) { | ||
System.err.println("Usage: COMMAND <app-info-file> <auth-file-output> <mode>") | ||
System.err.println("") | ||
System.err.println("<app-info-file>: a JSON file with information about your API app. Example:") | ||
System.err.println("") | ||
System.err.println(" {") | ||
System.err.println(" \"key\": \"Your Dropbox API app key...\",") | ||
System.err.println(" \"secret\": \"Your Dropbox API app secret...\"") | ||
System.err.println(" }") | ||
System.err.println("") | ||
System.err.println(" Get an API app key by registering with Dropbox:") | ||
System.err.println(" https://dropbox.com/developers/apps") | ||
System.err.println("") | ||
System.err.println("<auth-file-output>: If authorization is successful, the resulting API") | ||
System.err.println(" credential will be saved to this file, which can then be used with") | ||
System.err.println(" other example programs, such as the one in \"examples/account-info\".") | ||
System.err.println("") | ||
System.err.println("<mode>: value can only be short_live_token, pkce, scope, or incremental.") | ||
System.err.println(" short_live_token: authorization will request short_lived_token") | ||
System.err.println(" together with refresh token and expiration time.") | ||
System.err.println(" pkce: authorization will run short_live_token without app secret") | ||
System.err.println(" use that when you have a client side only app without server.") | ||
System.err.println(" scope: authorization will request specific scope.") | ||
System.err.println("") | ||
System.exit(1) | ||
return | ||
} | ||
val argAppInfoFile = args[0] | ||
val argAuthFileOutput = args[1] | ||
|
||
// Read app info file (contains app key and app secret) | ||
val appInfo: DbxAppInfo | ||
appInfo = try { | ||
DbxAppInfo.Reader.readFromFile(argAppInfoFile) | ||
} catch (ex: JsonReader.FileLoadException) { | ||
System.err.println("Error reading <app-info-file>: " + ex.message) | ||
System.exit(1) | ||
return | ||
} | ||
|
||
// Run through Dropbox API authorization process | ||
var authFinish: DbxAuthFinish? = null | ||
when (args[2]) { | ||
"short_live_token" -> authFinish = ShortLiveTokenAuthorize().authorize(appInfo) | ||
"pkce" -> authFinish = PkceAuthorize().authorize(appInfo) | ||
"scope" -> authFinish = ScopeAuthorize().authorize(appInfo) | ||
else -> { | ||
System.err.println("Error reading <mode> : " + args[2]) | ||
System.exit(1) | ||
} | ||
} | ||
println("Authorization complete.") | ||
println("- User ID: " + authFinish!!.userId) | ||
println("- Account ID: " + authFinish.accountId) | ||
println("- Access Token: " + authFinish.accessToken) | ||
println("- Expires At: " + authFinish.expiresAt) | ||
println("- Refresh Token: " + authFinish.refreshToken) | ||
println("- Scope: " + authFinish.scope) | ||
|
||
// Save auth information the new DbxCredential instance. It also contains app_key and | ||
// app_secret which is required to do refresh call. | ||
val credential = DbxCredential( | ||
authFinish.accessToken, authFinish | ||
.expiresAt, authFinish.refreshToken, appInfo.key, appInfo.secret | ||
) | ||
val output = File(argAuthFileOutput) | ||
try { | ||
DbxCredential.Writer.writeToFile(credential, output) | ||
println("Saved authorization information to \"" + output.canonicalPath + "\".") | ||
} catch (ex: IOException) { | ||
System.err.println("Error saving to <auth-file-out>: " + ex.message) | ||
System.err.println("Dumping to stderr instead:") | ||
DbxCredential.Writer.writeToStream(credential, System.err) | ||
System.exit(1) | ||
return | ||
} | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.