![](/img/trans.png)
[英]How to get upload progress with Java SDK for Dropbox API v2?
[英]How to auth automatically with Dropbox Java/Android SDK v2?
為了提高應用程序質量,我正在測試:單元測試和UI測試。 由於我在應用程序中具有Dropbox支持,因此我想對其進行測試,並且我需要在測試之前對Dropbox帳戶進行身份驗證(在我的Android應用程序中,用戶能夠保存文件,讀取文件,重命名等-基本文件例程)。
Dropbox為Java / Android SDK v2提供了示例,但即使命令行工具也需要一些手動操作-使用URL打開瀏覽器應用並選擇帳戶:
// Run through Dropbox API authorization process
DbxRequestConfig requestConfig = new DbxRequestConfig("examples-authorize");
DbxWebAuth webAuth = new DbxWebAuth(requestConfig, appInfo);
DbxWebAuth.Request webAuthRequest = DbxWebAuth.newRequestBuilder()
.withNoRedirect()
.build();
String authorizeUrl = webAuth.authorize(webAuthRequest);
System.out.println("1. Go to " + authorizeUrl);
System.out.println("2. Click \"Allow\" (you might have to log in first).");
System.out.println("3. Copy the authorization code.");
System.out.print("Enter the authorization code here: ");
String code = new BufferedReader(new InputStreamReader(System.in)).readLine();
if (code == null) {
System.exit(1); return;
}
code = code.trim();
DbxAuthFinish authFinish;
try {
authFinish = webAuth.finishFromCode(code);
} catch (DbxException ex) {
System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage());
System.exit(1); return;
}
System.out.println("Authorization complete.");
System.out.println("- User ID: " + authFinish.getUserId());
System.out.println("- Access Token: " + authFinish.getAccessToken());
是否可以在無需手動交互的情況下自動進行Dropbox身份驗證? 我希望提供應用密鑰/機密,帳戶電子郵件/密碼並獲取會話的accessToken
。
PS。 我想避免使用Robelectric + Espresso,並將其保留在單元/集成測試中,而不是在UI測試中。
不可以,Dropbox API沒有提供自動執行應用授權流程的方法。
請注意,盡管您可以存儲和重復使用訪問令牌,所以您可能只想手動為測試帳戶獲取一個令牌,然后重新使用它即可。
這是我的測試模板(希望對您有所幫助)。 您必須執行TODO
並至少手動兩次運行測試(粘貼身份驗證代碼和訪問令牌),然后才能進行測試。 接下來的所有測試調用都不需要手動執行任何操作。
public class DropboxFileSystemTest {
// credentials
private static final String APP_KEY = ""; // TODO : paste your app key
private static final String APP_SECRET = ""; // TODO : paste your app secret
// do run the test and follow the instructions
private static final String accountEmail = "test@domain.com"; // TODO : paste your test Dropbox account
private static String authCode; // TODO : run the test and paste auth code
private static String accessToken // TODO : run the test and paste access
private DbxAppInfo appInfo = new DbxAppInfo(APP_KEY, APP_SECRET);
// Run through Dropbox API authorization process
private DbxRequestConfig requestConfig = new DbxRequestConfig(DropboxFileSystemTest.class.getSimpleName());
private DbxWebAuth webAuth = new DbxWebAuth(requestConfig, appInfo);
private void startAuth() throws IOException {
DbxWebAuth.Request webAuthRequest = DbxWebAuth.newRequestBuilder()
.withNoRedirect()
.build();
String authorizeUrl = webAuth.authorize(webAuthRequest);
System.out.println("1. Go to " + authorizeUrl);
System.out.println("2. Click \"Allow\" (you might have to log in first). WARNING: log-in to " + accountEmail);
System.out.println("3. Copy the authorization code.");
System.out.println("4. Paste the authorization code to this test `this.authCode` value");
System.out.println("5. Re-run the test");
}
private DbxClientV2 client; // to be used for the requests
private void initWithAccessToken() {
DbxRequestConfig config = new DbxRequestConfig(UUID.randomUUID().toString());
client = new DbxClientV2(config, accessToken);
}
private void initAndVerifyAccount() throws DbxException {
initWithAccessToken();
// check expected account (trying to prevent user account to be wiped out)
DbxClientV2 client = DbxClientHolder.get().getClient();
FullAccount account = client.users().getCurrentAccount();
if (!account.getEmail().equals(accountEmail))
throw new RuntimeException("Wrong account: current is " + account.getEmail() + ", but " + accountEmail + " is expected");
}
private void clearFileSystem() throws FileSystemException {
// TODO : clear Dropbox file system
}
@Before
public void setUp() throws IOException, FileSystemException, DbxException {
auth();
clearFileSystem();
}
private void finishAuth() {
DbxAuthFinish authFinish;
try {
authFinish = webAuth.finishFromCode(authCode);
} catch (DbxException ex) {
System.err.println("Error in DbxWebAuth.authorize: " + ex.getMessage());
System.exit(1); return;
}
System.out.println("Authorization complete.");
System.out.println("- User ID: " + authFinish.getUserId());
System.out.println("- Access Token: " + authFinish.getAccessToken());
System.out.println();
System.out.println("1. Copy the access token");
System.out.println("2. Paste the access token to this test `this.accessToken` value");
System.out.println("3. Re-run the test");
accessToken = authFinish.getAccessToken();
}
private void auth() throws IOException, FileSystemException, DbxException {
if (accessToken == null) {
if (authCode != null ) {
finishAuth();
throw new RuntimeException("Manual actions required: copy-paste access token");
} else {
startAuth();
throw new RuntimeException("Manual actions required: copy-paste authCode");
}
} else {
initAndVerifyAccount();
}
}
@After
public void tearDown() throws FileSystemException {
if (client != null) {
clearFileSystem();
}
}
@Test
public void testSmth() {
// TODO : write your test using `this.client` for requests
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.