我有什么
我的照片需要发布到Social API
,如FB,TWITTER,TUMBLR,FLICKR
我想要什么
我想将照片发布到Tumblr
我尝试了什么
我遇到了Jumblr Client
Tumblr
我的问题
该文件直接说
JumblrClient client = new JumblrClient("consumer_key","consumer_secret");
client.setToken("oauth_token", "oauth_token_secret");
我有consumer_key
& consumer_secret
,如何验证并获取 oauth_token,oauth_token_secret
答案 0 :(得分:3)
我必须承认,缺少Jumblr文档。
请参阅以下链接。他们会回答你的问题
1)完成登录程序代码:Github Project
2)将图像发布到Tumblr的代码片段:Code snippet
答案 1 :(得分:1)
感谢Clinkz。这些链接的位置相当不错,但是如果仍然有人遇到问题,这是我在 Android应用中使用的代码,并且可以正常工作。 对于Android ,我们必须在应用级别gradle文件中添加以下两行:
实施'com.daksh:loglr:2.1.4'
实施'com.tumblr:jumblr:0.0.13'
private void loginToTumblr() {
Loglr loglr = Loglr.INSTANCE;
if(loglr != null) {
loglr.setConsumerKey("TUBMLR_CONSUMER_KEY"); // Replace with your App's Key
loglr.setConsumerSecretKey("TUBMLR_CONSUMER_SECRET"); // Replace with your App's Key
loglr.setUrlCallBack("https://www.callback.com/"); // Replace with your App's Callback
loglr.setLoginListener(new LoginListener() {
@Override
public void onLoginSuccessful(com.tumblr.loglr.LoginResult loginResult) {
String oAuthToken = loginResult.getOAuthToken();
String oAuthTokenSecret = loginResult.getOAuthTokenSecret();
Log.w(TAG, oAuthToken + "\n" + oAuthTokenSecret);
shareOnTumblr(oAuthToken , oAuthTokenSecret);
}
});
loglr.setExceptionHandler(new ExceptionHandler() {
@Override
public void onLoginFailed(RuntimeException e) {
Log.e(TAG, "Loglr Exeception: " + e.getMessage());
Toast.makeText(this, "Sorry! can't login to the tumblr.", Toast.LENGTH_LONG).show();
}
});
loglr.initiate(this);
}
else
Toast.makeText(this, "Something went wrong while Loggin in to Tumblr.", Toast.LENGTH_LONG).show();
}
private void loginToTumblr() {
Loglr loglr = Loglr.INSTANCE;
if(loglr != null) {
loglr.setConsumerKey("TUBMLR_CONSUMER_KEY"); // Replace with your App's Key
loglr.setConsumerSecretKey("TUBMLR_CONSUMER_SECRET"); // Replace with your App's Key
loglr.setUrlCallBack("https://www.callback.com/"); // Replace with your App's Callback
loglr.setLoginListener(new LoginListener() {
@Override
public void onLoginSuccessful(com.tumblr.loglr.LoginResult loginResult) {
String oAuthToken = loginResult.getOAuthToken();
String oAuthTokenSecret = loginResult.getOAuthTokenSecret();
Log.w(TAG, oAuthToken + "\n" + oAuthTokenSecret);
shareOnTumblr(oAuthToken , oAuthTokenSecret);
}
});
loglr.setExceptionHandler(new ExceptionHandler() {
@Override
public void onLoginFailed(RuntimeException e) {
Log.e(TAG, "Loglr Exeception: " + e.getMessage());
Toast.makeText(this, "Sorry! can't login to the tumblr.", Toast.LENGTH_LONG).show();
}
});
loglr.initiate(this);
}
else
Toast.makeText(this, "Something went wrong while Loggin in to Tumblr.", Toast.LENGTH_LONG).show();
}
登录后,如果您想共享一些带有标题的图像,可以使用以下代码:
private void shareOnTumblr(String token, String tokenSecret) {
IResult iResult = new IResult() {
@Override
public void onSuccess(String result) {
loadingBar.hide();
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}
@Override
public void onError(String error) {
Toast.makeText(this, error, Toast.LENGTH_LONG).show();
}
};
String caption = "IMAGE CAPTION GOES HERE";
String imagePath = "IMAGE PATH GOES HERE"; //It must a String
String params[] = new String[]{token, tokenSecret , caption, imagePath};
new TumblrPostAsyncTask(iResult).execute(params);
}
private void shareOnTumblr(String token, String tokenSecret) {
IResult iResult = new IResult() {
@Override
public void onSuccess(String result) {
loadingBar.hide();
Toast.makeText(this, result, Toast.LENGTH_LONG).show();
}
@Override
public void onError(String error) {
Toast.makeText(this, error, Toast.LENGTH_LONG).show();
}
};
String caption = "IMAGE CAPTION GOES HERE";
String imagePath = "IMAGE PATH GOES HERE"; //It must a String
String params[] = new String[]{token, tokenSecret , caption, imagePath};
new TumblrPostAsyncTask(iResult).execute(params);
}
接口看起来像这样:
public static class TumblrPostAsyncTask extends AsyncTask<String, String, Boolean> {
IResult iResult;
TumblrPostAsyncTask(IResult iResult){
this.iResult = iResult;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... params) {
boolean result;
try {
JumblrClient client = new JumblrClient("TUBMLR_CONSUMER_KEY", "TUBMLR_CONSUMER_SECRET");
client.setToken(params[0], params[1]);
PhotoPost post = client.newPost(client.user().getBlogs().get(0).getName(),PhotoPost.class);
Log.w(TAG, params[2] + " " + params[3]);
post.setCaption(params[2]);
post.setData(new File(params[3]));
post.save();
result = true;
} catch (Exception e) {
Log.w(TAG, "Tumblr's Error: " + e.getMessage());
result = false;
}
return result;
}
@Override
protected void onPostExecute(Boolean success) {
super.onPostExecute(success);
Log.w(TAG, "On Post Execute: " + success);
if(success)
iResult.onSuccess("Successfully Posted on Tumblr.");
else
iResult.onError("Sorry! couldn't Post on Tumblr.");
}
}
public static class TumblrPostAsyncTask extends AsyncTask<String, String, Boolean> {
IResult iResult;
TumblrPostAsyncTask(IResult iResult){
this.iResult = iResult;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Boolean doInBackground(String... params) {
boolean result;
try {
JumblrClient client = new JumblrClient("TUBMLR_CONSUMER_KEY", "TUBMLR_CONSUMER_SECRET");
client.setToken(params[0], params[1]);
PhotoPost post = client.newPost(client.user().getBlogs().get(0).getName(),PhotoPost.class);
Log.w(TAG, params[2] + " " + params[3]);
post.setCaption(params[2]);
post.setData(new File(params[3]));
post.save();
result = true;
} catch (Exception e) {
Log.w(TAG, "Tumblr's Error: " + e.getMessage());
result = false;
}
return result;
}
@Override
protected void onPostExecute(Boolean success) {
super.onPostExecute(success);
Log.w(TAG, "On Post Execute: " + success);
if(success)
iResult.onSuccess("Successfully Posted on Tumblr.");
else
iResult.onError("Sorry! couldn't Post on Tumblr.");
}
}