Dropbox的自动验证使用核心API [英] Dropbox auto authenticate using core APi
问题描述
我想避免用户现在的code看起来像这样从浏览器登录。
要避免登录屏幕mApi.getSession()startAuthentication(本)。 - 这将揭开序幕登录我已删除该硬codeD令牌密钥和令牌密钥
I want to avoid a user to login from browser right now the code looks like this. Want to avoid login screen mApi.getSession().startAuthentication(this); -- this will kick off login i have removed this and hard coded token key and token secret key
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
AndroidAuthSession session = buildSession();
mApi = new DropboxAPI<AndroidAuthSession>(session);
checkAppKeySetup();
} catch (Exception e) {
Logger.Error("onCreate", e);
}
}
protected void onResume() {
super.onResume();
// Dropbox
AndroidAuthSession session = mApi.getSession();
if (session.authenticationSuccessful()) {
try {
// Mandatory call to complete the auth
session.finishAuthentication();
// Store it locally in our app for later use
TokenPair tokens = session.getAccessTokenPair();
storeKeys(tokens.key, tokens.secret);
setLoggedIn(true);
} catch (IllegalStateException e) {
Toast.makeText(
this,
"Couldn't authenticate with Dropbox:"
+ e.getLocalizedMessage(), Toast.LENGTH_SHORT)
.show();
Logger.Error("onCreate", e);
}
Log.i("On resume method", "authentication was active");
}
}
private AndroidAuthSession buildSession() {
AppKeyPair appKeyPair = new AppKeyPair(APP_KEY, APP_SECRET);
AndroidAuthSession session;
String[] stored = getKeys();
if (stored != null) {
AccessTokenPair accessToken = new AccessTokenPair(stored[0],
stored[1]);
session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE,
accessToken);
} else {
session = new AndroidAuthSession(appKeyPair, ACCESS_TYPE);
}
private String[] getKeys() {
SharedPreferences prefs = getSharedPreferences(ACCOUNT_PREFS_NAME, 0);
String key = prefs.getString(ACCESS_KEY_NAME, "XXXXXXXX");
String secret = prefs.getString(ACCESS_SECRET_NAME, "xxxxxxx");
if (key != null && secret != null) {
String[] ret = new String[2];
ret[0] = key;
ret[1] = secret;
return ret;
} else {
return null;
}
}
session.authenticationSuccessful()总是返回false,我不能够将文件上传到Dropbox的。在此先感谢
session.authenticationSuccessful() returns false always and i am not able to upload a file to dropbox. Thanks in advance
推荐答案
我觉得你只是想这样的:
I think you just want this:
// OAUTH_TOKEN and OAUTH_TOKEN_SECRET are the hardcoded credentials you want to use
mApi = new DropboxAPI<AndroidAuthSession>(new AndroidAuthSession(
new AppKeyPair(APP_KEY, APP_SECRET), ACCESS_TYPE,
new AccessTokenPair(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)));
但的我强烈反对这种做法的。如果你硬code OAuth凭证为您的帐户,那么任何人谁拥有你的应用程序将能够提取令牌,然后删除所有文件,请使用您的帐号共享非法内容,等等,总之,这是一个非常糟糕的主意。
But I strongly advise against this approach. If you hardcode an OAuth token for your account, then anyone who has your app will be able to extract the token and then delete all your files, use your account to share illegal content, etc. In short, it's a very bad idea.
典型的使用Dropbox的API的是让每个用户使用你的应用程序自己的账户。
The typical use of the Dropbox API is to have each user use their own account with your app.
这篇关于Dropbox的自动验证使用核心API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!