问题
Why following code giving me such type of exception com.tumblr.jumblr.exceptions.JumblrException: Not Authorized
I saw this question , but i am not able to resolved this issue...plz help me
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "TumblrDemo";
private static final String PREFERENCE_NAME = "tumblr";
public static final String REQUEST_TOKEN_URL = "https://www.tumblr.com/oauth/request_token";
public static final String ACCESS_TOKEN_URL = "https://www.tumblr.com/oauth/access_token";
public static final String AUTH_URL = "https://www.tumblr.com/oauth/authorize";
// public static final String CALLBACK_URL =
// "tumblrdemo://tumblrdemo.com/ok";
public static final String OAUTH_CALLBACK_SCHEME = "oauthflow-tumblr";
public static final String OAUTH_CALLBACK_HOST = "callback";
public static final String CALLBACK_URL = OAUTH_CALLBACK_SCHEME + "://"
+ OAUTH_CALLBACK_HOST;
private TransparentProgressDialog progressDialog;
private Button loginTumblrBtn;
private SharedPreferences preferences;
private CommonsHttpOAuthConsumer consumer;
private CommonsHttpOAuthProvider provider;
private String token, token_secret, oauth_verifier;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
consumer = new CommonsHttpOAuthConsumer(Constant.CONSUMER_KEY,
Constant.CONSUMER_SECRET);
provider = new CommonsHttpOAuthProvider(REQUEST_TOKEN_URL,
ACCESS_TOKEN_URL, AUTH_URL);
preferences = getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
loginTumblrBtn = (Button) findViewById(R.id.login_tumblr);
Uri uri = this.getIntent().getData();
if (uri != null && uri.getScheme().equals(OAUTH_CALLBACK_SCHEME)) {
loginTumblrBtn.setText(getString(R.string.logout_tumblr));
try {
oauth_verifier = uri.getQueryParameter(OAuth.OAUTH_VERIFIER);
Log.d(TAG, uri.toString());
// provider.retrieveAccessToken(consumer, oauth_verifier);
// token = consumer.getToken();
// token_secret = consumer.getTokenSecret();
token = uri.getQueryParameter("oauth_token");
token_secret = uri.getQueryParameter("oauth_verifier");
consumer.setTokenWithSecret(token, token_secret);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(Constant.TOKEN, token);
editor.putString(Constant.TOKEN_SECRET, token_secret);
editor.commit();
getUserDetails();
} catch (Exception e) {
e.printStackTrace();
}
} else {
loginTumblrBtn.setText(getString(R.string.login_tumblr));
}
loginTumblrBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.login_tumblr) {
if (isTumblrConnected()) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString(Constant.TOKEN, null);
editor.putString(Constant.TOKEN_SECRET, null);
editor.commit();
loginTumblrBtn.setText(getString(R.string.login_tumblr));
} else {
new AsyncTaskClass().execute();
}
}
}
private boolean isTumblrConnected() {
return preferences.getString(Constant.TOKEN, null) != null;
}
private class AsyncTaskClass extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
String authUrl = provider.retrieveRequestToken(consumer,
CALLBACK_URL);
startActivity(new Intent("android.intent.action.VIEW",
Uri.parse(authUrl)));
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new TransparentProgressDialog(MainActivity.this,
R.drawable.loading);
progressDialog.show();
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (progressDialog != null && progressDialog.isShowing())
progressDialog.dismiss();
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (isTumblrConnected()) {
token = preferences.getString(Constant.TOKEN, null);
token_secret = preferences.getString(Constant.TOKEN_SECRET, null);
loginTumblrBtn.setText(getString(R.string.logout_tumblr));
getUserDetails();
} else {
loginTumblrBtn.setText(getString(R.string.login_tumblr));
}
}
private void getUserDetails() {
new Thread(new Runnable() {
@Override
public void run() {
try {
JumblrClient client = new JumblrClient(
Constant.CONSUMER_KEY, Constant.CONSUMER_SECRET);
client.setToken(token, token_secret);
// Write the user's name
User user = client.user();
System.out.println(user.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
回答1:
Ya finally got solution after too much work, i misunderstood between access token and request token.
Here it is working solution for above issue.Hope it works for you.
public class MainActivity extends Activity implements OnClickListener {
private Button loginTumblrBtn;
private CommonsHttpOAuthConsumer consumer;
private CommonsHttpOAuthProvider provider;
private SharedPreferences preferences;
private Uri uri;
private ProgressDialog progressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences = getSharedPreferences("tumblr", Context.MODE_PRIVATE);
loginTumblrBtn = (Button) findViewById(R.id.login_tumblr);
loginTumblrBtn.setOnClickListener(this);
consumer = new CommonsHttpOAuthConsumer(Constant.CONSUMER_KEY,
Constant.CONSUMER_SECRET);
provider = new CommonsHttpOAuthProvider(Constant.REQUEST_TOKEN_URL,
Constant.ACCESS_TOKEN_URL, Constant.AUTH_URL);
uri = this.getIntent().getData();
if (uri != null
&& uri.getScheme().equals(Constant.OAUTH_CALLBACK_SCHEME)) {
loginTumblrBtn.setText(getString(R.string.logout_tumblr));
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
consumer.setTokenWithSecret(
preferences.getString("requestToken", ""),
preferences.getString("requestSecret", ""));
provider.setOAuth10a(true);
provider.retrieveAccessToken(consumer,
uri.getQueryParameter(OAuth.OAUTH_VERIFIER));
consumer.setTokenWithSecret(consumer.getToken(),
consumer.getTokenSecret());
SharedPreferences.Editor editor = preferences.edit();
editor.putString("token", consumer.getToken());
editor.putString("token_secret",
consumer.getTokenSecret());
editor.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
loginTumblrBtn.setText(getString(R.string.login_tumblr));
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login_tumblr:
if (preferences.getString("token", null) != null) {
loginTumblrBtn.setText(getString(R.string.login_tumblr));
SharedPreferences.Editor editor = preferences.edit();
editor.putString("token", null);
editor.putString("token_secret", null);
editor.commit();
} else {
progressDialog = ProgressDialog.show(this, "Loading",
"Please Wait...");
new Thread(new Runnable() {
@Override
public void run() {
try {
String authUrl = provider.retrieveRequestToken(
consumer, Constant.CALLBACK_URL);
SharedPreferences.Editor editor = preferences
.edit();
editor.putString("requestToken",
consumer.getToken());
editor.putString("requestSecret",
consumer.getTokenSecret());
editor.commit();
startActivity(new Intent(
"android.intent.action.VIEW", Uri
.parse(authUrl)));
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
break;
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (preferences.getString("token", null) != null) {
loginTumblrBtn.setText(getString(R.string.logout_tumblr));
} else {
loginTumblrBtn.setText(getString(R.string.login_tumblr));
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
and put this intent-filter code inside manifest>application>activity (where callback is done)
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="callback"
android:scheme="oauthflow-tumblr" />
</intent-filter>
Image sharing on TUMBLR
public void shareImageOnTumblr(final File imgFile, final String caption,
final Handler handler) {
new Thread(new Runnable() {
@Override
public void run() {
try {
PhotoPost photoPost = client.newPost(client.user()
.getBlogs().get(0).getName(), PhotoPost.class);
if (!caption.isEmpty())
photoPost.setCaption(caption);
photoPost.setPhoto(new Photo(imgFile));
photoPost.save();
Bundle bundle = new Bundle();
Message message = new Message();
bundle.putInt("method",
UploadActivity.SHARED_PHOTO_SUCCESSFULLY);
message.setData(bundle);
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
Video sharing on TUMBLR
public void shareVideoOnTumblr(final File videoFile, final String caption,
final Handler handler) {
new Thread(new Runnable() {
@Override
public void run() {
try {
VideoPost videoPost = client.newPost(client.user()
.getBlogs().get(0).getName(), VideoPost.class);
if (!caption.toString().isEmpty())
videoPost.setCaption(caption.toString());
videoPost.setData(videoFile);
videoPost.save();
Bundle bundle = new Bundle();
Message message = new Message();
bundle.putInt("method",
UploadActivity.SHARED_VIDEO_SUCCESSFULLY);
message.setData(bundle);
handler.sendMessage(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
来源:https://stackoverflow.com/questions/25562436/android-tumblr-login-issue-jumblrexception-not-authorized