我已经尝试了许多不同的方法,并在互联网上到处搜索,以找到在OAuth中使用JTwitter的教程。下面是我完成的以下步骤
下载Jtwitter和Signpost,将它们作为Jars添加到Java Builder中
创建了一个简单的按钮,可以运行
public class ShareGenerator extends Activity {
private static final String JTWITTER_OAUTH_KEY = "this_is_populated";
private static final String JTWITTER_OAUTH_SECRET = "this_is_populated";
Button menupopButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.share);
this.setContentView(R.layout.share);
this.txShare = (TextView)this.findViewById(R.id.lblshare);
this.menupopButton = (Button)this.findViewById(R.id.menupop);
menupopButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
TwitterSend();
}
});
}我要上我的课
public void TwitterSend () {
OAuthSignpostClient client = new OAuthSignpostClient(JTWITTER_OAUTH_KEY, JTWITTER_OAUTH_SECRET, "oob");
Twitter jtwit = new Twitter("bob", client);
// open the authorisation page in the user's browser
client.authorizeDesktop();
// get the pin
String v = client.askUser("Please enter the verification PIN from Twitter");
client.setAuthorizationCode(v);
// Optional: store the authorisation token details
Object accessToken = client.getAccessToken();
// use the API!
jtwit.setStatus("Messing about in Java");
}然而,我甚至无法弹出OAuth屏幕。当它到达那里时就会崩溃。有人能至少帮我看看OAuth屏幕吗?我确实设置了正确的导入。
发布于 2010-07-09 15:31:51
问题出在这一行,它使用了java.awt.Desktop:
// open the authorisation page in the user's browser
client.authorizeDesktop();这可以在台式PC上运行,但不能在Android上运行。
取而代之的是,从client.authorizeUrl();获取url并将用户发送到那里。例如,像这样:
URI url = client.authorizeUrl();
Intent myIntent = new Intent(Intent.VIEW_ACTION);
myIntent.setData(url);
startActivity(myIntent);但我不是Android程序员!几乎可以肯定的是,使用回调而不是oob可以做得更好。希望其他人能提供这方面的代码...
发布于 2011-10-19 20:08:49
Daniel提供了一个很好的起点。这是我在我的Android应用程序中实现的方式:
OAuthSignpostClient authClient = new OAuthSignpostClient('apiKey','apiSecret','callbackUrl');
java.net.URI jUrl = authClient.authorizeUrl();
Uri.Builder uriBuilder = new Uri.Builder();
uriBuilder.encodedPath(jUrl.toString());
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(jUrl.toString()));
startActivity(myIntent);当然,为了简洁起见,我使用了'apiKey'等。您需要在OAuthSignpostClient构造函数中使用自己的密钥、密钥和回调url。
注意:您需要将JTwitter提供的Java.net.URI转换为Android.net.Uri,才能使用它来启动新的意图。
在此之后,您将需要使用一个意图过滤器来捕获回调url,并对您将从Twitter API获得的用户令牌和密钥执行一些操作。
发布于 2010-11-17 15:35:09
如果你关心,请查看这篇文章的版本历史。
https://stackoverflow.com/questions/3200505
复制相似问题