【问题标题】:Simple JTwitter for Android example用于 Android 的简单 JTwitter 示例
【发布时间】:2011-03-13 03:29:43
【问题描述】:

我尝试了许多不同的方法,并在整个互联网上搜索以找到将 JTwitter 与 OAuth 结合使用的教程。这是我完成的以下步骤

同时下载 Jtwitter 和 Signpost 在 Java Builder 中将它们作为 Jars 添加

创建了一个可以运行的简单按钮

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 屏幕?我确实正确设置了导入。

【问题讨论】:

  • 查看 Logcat 关于崩溃的信息,并确保您在清单中拥有 INTERNET 权限。
  • Logcat 说 java.lang.NoClassDefFoundErrors: java.awt.Desktop 它在 client.authorizeDesktop() 处失败;
  • 我无法将 URI 转换为 Uri 以便在 setData() 方法中传递 url。有什么建议吗?

标签: android oauth jtwitter


【解决方案1】:

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 才能使用它来启动新的 Intent。

在此之后,您将需要使用意图过滤器来捕获回调 url,并使用您将从 Twitter API 获得的用户令牌和秘密做一些事情。

【讨论】:

  • 我有点困惑,为什么你有两行不使用的 uriBuilder。编码路径有隐藏的后果还是我错过了什么?
【解决方案2】:

请参阅此帖子 [如果您关心,请查看修订历史记录]

【讨论】:

  • 你会找到每一个相关的问题并提供相同的答案,不是吗?
【解决方案3】:

问题出在这一行,它使用了java.awt.Desktop:

// open the authorisation page in the user's browser
client.authorizeDesktop();

这适用于台式电脑,但不适用于 Android。

取而代之的是,从client.authorizeUrl(); 获取网址并将用户发送到那里。例如。像这样:

URI url = client.authorizeUrl();
Intent myIntent = new Intent(Intent.VIEW_ACTION);
myIntent.setData(url);
startActivity(myIntent);

但我不是 Android 编码员!通过使用回调而不是oob,您几乎可以肯定做得更好。希望其他人可以提供代码...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-27
    • 1970-01-01
    • 1970-01-01
    • 2015-06-21
    • 1970-01-01
    • 2016-02-19
    • 1970-01-01
    • 2016-02-02
    相关资源
    最近更新 更多