【问题标题】:Android Jtwitter, authentication issuesAndroid Jtwitter,身份验证问题
【发布时间】:2011-10-11 03:03:56
【问题描述】:

您好,我在使用 JTwitter 功能对我的 Twitter 应用程序进行身份验证时遇到困难。我总是收到“TwitterException”

这是我的方法

OAuthSignpostClient oauthClient = new OAuthSignpostClient(consumerKey, 
            privateKey, "oob");

a) 我不知道“oob”值应该是什么,它是“callbackURL”,在我的推特应用程序中它显示"callBack URL: none",所以我尝试输入"none""None"null 其中"oob" 没有不同的结果。

剩下的就是样板文件

 Intent i = new Intent(Intent.ACTION_VIEW, 
    Uri.parse(oauthClient.authorizeUrl().toString()));
    startActivity(i);
    // get the pin
    String v = oauthClient.askUser("Please enter the verification PIN from Twitter");
    oauthClient.setAuthorizationCode(v);
    // Store the authorisation token details for future use
    String[] accessToken = oauthClient.getAccessToken();
    // Next time we can use new OAuthSignpostClient(OAUTH_KEY, OAUTH_SECRET, 
    //        accessToken[0], accessToken[1]) to avoid authenticating again.

    EditText twitterText = (EditText)findViewById(R.id.twitterText);
    twitterText.getText();

    // Make a Twitter object
    Twitter twitter = new Twitter(null, oauthClient);
    // Print Daniel Winterstein's status
    //System.out.println(twitter.getStatus("winterstein"));
    // Set my status
    twitter.setStatus(twitterText.getText());

在这一点上,我只是不确定如何进行这项工作。希望我可以更详细地说,但这与身份验证有关。我在网上看到的东西没有帮助

【问题讨论】:

    标签: android authentication twitter oauth-2.0 jtwitter


    【解决方案1】:

    试试这个作为你的回调网址

    OAuthSignpostClient oauthClient = 
        new OAuthSignpostClient(consumerKey, privateKey, "callback://twitter");  
    

    记住!之后:

        startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
    

    执行以下操作

    覆盖onResume()获取验证码

    protected void onResume() {
        super.onResume();
        if (this.getIntent()!=null && this.getIntent().getData()!=null){
            Uri uri = this.getIntent().getData();
            if (uri != null && uri.toString().startsWith("callback://twitter")) {
                //Do whatever you want
                //usually use uri.getQueryParameter(someString);
                //test what could be someString using Log.v("",uri.toString());
                //you want the Authorization code which is a couple of numbers
                //so you could use oauthClient.setAuthorizationCode(v); 
                //and finally initialise Twitter
            }
        }
    }
    

    您可以使用uri.getQueryParameterNames() 获取参数String 名称。


    @BobVork

    我想补充一点,您需要在“设置”选项卡中为您的 Twitter 应用程序(在 twitter.com 上)设置一个回调 URL。

    你在字段中输入什么都没有关系,但如果它是空的,回调 url 将不起作用。

    【讨论】:

    • 我想补充一点,您需要在您的 Twitter 应用程序(在 twitter.com 上)的“设置”选项卡中设置回调 URL。您在字段中输入的内容甚至都没有关系,但如果它为空,则回调 url 将不起作用。我花了大约一个小时才找到答案,所以我认为这可能会为其他人节省一些时间。
    【解决方案2】:

    我不确定这是问题所在,但您似乎从未为 twitter 对象设置用户名。

    【讨论】:

    • 我认为宣誓的目的是让用户不必输入他们的用户名或其他东西......更不用说硬编码了?这个概念有点混乱,我的第一个直觉是用户名在某个地方,但随后 twitter 对象似乎不需要它,并且有人誓言会处理它
    • 我对 twitter API 不是很熟悉,但是我所看到的所有地方(即使在您从中获取代码的示例中),即使使用了 oAuth,也使用了用户名。可以保存一次,需要时使用,不必每次都从某个TextView中读取。
    【解决方案3】:

    嗯,我不知道 Jtwitter API 是怎么回事,而且我现在还没有足够的 Java 知识来真正理解这段代码的内部工作原理,但是你尝试过,而不是 NULL 或 @ 987654322@,改为尝试?。这是从 Javascript 进行调用时与 restful API 一起使用的回调。试试看。

    【讨论】:

      猜你喜欢
      • 2015-05-28
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 1970-01-01
      • 2013-07-23
      • 2015-08-25
      • 2010-11-17
      相关资源
      最近更新 更多