无法使用 twitter4j 授权我的应用程序
我正在使用 twitter4j 2.2.4 编写一个 Android twitter 应用程序,但在通过 OAuth 时遇到了困难。
这是我的代码:
package info.mahmoudhossam.twitter;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.auth.AccessToken;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class TwitterActivity extends Activity {
private static int OAUTH_REQUEST = 1;
private static String consumerKey = "##########";
private static String consumerSecret = "################";
private Twitter twitter;
private AccessToken accessToken;
private TwitterBackend backend;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button loginButton = (Button) findViewById(R.id.button1);
registerForContextMenu(loginButton);
backend = new TwitterBackend();
twitter = backend.getTwitterInstance(consumerKey, consumerSecret);
}
public void onLogin(View view) {
Intent intent = new Intent("mahmoud.browser");
try {
Log.i("URL", backend.getRequestToken().getAuthenticationURL());
intent.putExtra("url", backend.getRequestToken().getAuthenticationURL());
} catch (TwitterException e) {
Log.e("Twitter", e.getMessage());
return;
}
startActivityForResult(intent, OAUTH_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == OAUTH_REQUEST && resultCode == RESULT_OK) {
Uri url = Uri.parse(data.getExtras().getString("url"));
String verifier = url.getQueryParameter("oauth_verifier");
Log.i("Verifier", verifier);
try {
accessToken = backend.getAccessToken(verifier);
} catch (TwitterException e) {
Log.e("Twitter", "Exception occurred, quitting");
return;
}
twitter.setOAuthAccessToken(accessToken);
try {
if(twitter.verifyCredentials() != null){
Toast.makeText(getApplicationContext(), "Logged in!", Toast.LENGTH_SHORT);
}
} catch (TwitterException e) {
Log.e("Twitter", e.getMessage());
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this,
"Cannot connect to twitter, app not authorized",
Toast.LENGTH_SHORT).show();
}
}
}
TwitterBackend 类:
package info.mahmoudhossam.twitter;
import java.util.List;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
public class TwitterBackend {
private static Twitter twitter;
public Twitter getTwitterInstance(String consumerKey,
String consumerSecret){
if(twitter == null){
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumerKey, consumerSecret);
}
return twitter;
}
public RequestToken getRequestToken()
throws TwitterException {
return twitter.getOAuthRequestToken();
}
public AccessToken getAccessToken(String verifier)
throws TwitterException{
return twitter.getOAuthAccessToken(getRequestToken(), verifier);
}
public List<Status> getTimeline() throws TwitterException{
return twitter.getHomeTimeline();
}
public void updateStatus(String status) throws TwitterException{
twitter.updateStatus(status);
}
}
我从 Twitter 收到 401 错误,指出身份验证凭据丢失或不正确,并且我确定我已输入正确的消费者密钥/秘密。
代码本身有问题吗?
I'm writing an Android twitter app using twitter4j 2.2.4, and I'm having trouble getting past OAuth.
here's my code :
package info.mahmoudhossam.twitter;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.auth.AccessToken;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class TwitterActivity extends Activity {
private static int OAUTH_REQUEST = 1;
private static String consumerKey = "##########";
private static String consumerSecret = "################";
private Twitter twitter;
private AccessToken accessToken;
private TwitterBackend backend;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button loginButton = (Button) findViewById(R.id.button1);
registerForContextMenu(loginButton);
backend = new TwitterBackend();
twitter = backend.getTwitterInstance(consumerKey, consumerSecret);
}
public void onLogin(View view) {
Intent intent = new Intent("mahmoud.browser");
try {
Log.i("URL", backend.getRequestToken().getAuthenticationURL());
intent.putExtra("url", backend.getRequestToken().getAuthenticationURL());
} catch (TwitterException e) {
Log.e("Twitter", e.getMessage());
return;
}
startActivityForResult(intent, OAUTH_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == OAUTH_REQUEST && resultCode == RESULT_OK) {
Uri url = Uri.parse(data.getExtras().getString("url"));
String verifier = url.getQueryParameter("oauth_verifier");
Log.i("Verifier", verifier);
try {
accessToken = backend.getAccessToken(verifier);
} catch (TwitterException e) {
Log.e("Twitter", "Exception occurred, quitting");
return;
}
twitter.setOAuthAccessToken(accessToken);
try {
if(twitter.verifyCredentials() != null){
Toast.makeText(getApplicationContext(), "Logged in!", Toast.LENGTH_SHORT);
}
} catch (TwitterException e) {
Log.e("Twitter", e.getMessage());
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this,
"Cannot connect to twitter, app not authorized",
Toast.LENGTH_SHORT).show();
}
}
}
And the TwitterBackend class :
package info.mahmoudhossam.twitter;
import java.util.List;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
public class TwitterBackend {
private static Twitter twitter;
public Twitter getTwitterInstance(String consumerKey,
String consumerSecret){
if(twitter == null){
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(consumerKey, consumerSecret);
}
return twitter;
}
public RequestToken getRequestToken()
throws TwitterException {
return twitter.getOAuthRequestToken();
}
public AccessToken getAccessToken(String verifier)
throws TwitterException{
return twitter.getOAuthAccessToken(getRequestToken(), verifier);
}
public List<Status> getTimeline() throws TwitterException{
return twitter.getHomeTimeline();
}
public void updateStatus(String status) throws TwitterException{
twitter.updateStatus(status);
}
}
I'm getting a 401 error from twitter saying that authentication credentials are missing or incorrect, and I'm sure I've entered the correct consumer key/secret.
Is there something wrong with the code itself?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我意识到这是一个老问题,但我使用了您的 TwitterBackend 类并且遇到了同样的问题。我通过做一个小的改变解决了这个问题(不能说为什么它有效,你的应该没问题)。
将此更改
为此
我无法解释原因,但我刚刚开始查看 twitter4j 上列出的每个方法 getOAuthAccessToken() 并得到了一个有效的。
我希望这对某人有帮助,但感谢您提供了很好的基础课程。
I realize this is an old question, but I used your
TwitterBackend
class and I had the same issue. I resolved this by making one minor change (can't say why it works, yours should have been fine).Change this
To this
I'm unable to explain why, but I just started going down each method listed at twitter4j for getOAuthAccessToken() and got one that works.
I hope this helps someone, but thank you for the nice base class to work with.
我不认为这是你的代码的问题。这是整个
Twitter
授权机制的问题,似乎如果您从手机上的Twitter App
登录,就会进入Twitter
那么OAuth
授权会给你这个错误。我猜它会获取 Twitter 应用程序使用的凭据并尝试使用它们访问该应用程序,这就是您收到“凭据错误”消息的原因。您可以尝试从手机的
Twitter 应用
注销,然后使用OAuth
重试。它对我有用,但当您登录Twitter 应用
并且您还尝试使用OAuth
从您的应用登录时,我仍然找不到解决问题的方法>。I don't think this is a problem with your code. This is a problem with the whole
Twitter
authorisation mechanism, it seems that if your are logged in from theTwitter App
on your phone, intoTwitter
then theOAuth
authorization gives you this error. I guess it takes the credentials used by theTwitter App
and tries to acces the App using them, and that's why you get the "incorrect credentials" message.You can try to logout from the
Twitter App
of your phone, and then try again withOAuth
. It worked for me, but I still couldn't find how to solve the problem when you are logged into theTwitter App
and you also try to log in from your app usingOAuth
.如果有人遇到这个问题,我找到了解决问题的方法。
这是模拟器的时钟不同步,从用于弄乱时钟的快照启动模拟器。
因此,如果您遇到此问题,请检查模拟器的时间和日期,如果不同步,请更正它。
In case someone gets stuck with this, I found a solution to my problem.
It was the emulator's clock being out of sync, starting emulators from snapshots used to mess up the clock.
So if you have this problem, check the emulator's time and date, and if it's out of sync, correct it.