【问题标题】:Everythings seems ok but function retrieveRequestToken() throws 'OAuthCommunicationException' exception一切似乎都很好,但函数 retrieveRequestToken() 抛出“OAuthCommunicationException”异常
【发布时间】:2012-02-05 10:37:36
【问题描述】:

我正在尝试构建一个 android 应用程序,以便它在 twitter 上发布推文。
我正在尝试使用函数 retrieveRequestToken(consumer,CALLBACK_URI) 获取 uri,但它会引发“OAuthCommunicationException”异常。 我给了应用程序的互联网使用权限(我已经检查过了)。我已经在 twitter 上注册了应用程序,提供读、写和直接消息。

Logcat
01-07 192624.589 Din(581) check1
01-07 192624.691 Dcommunication(581) 4
01-07 192624.691 WSystem.err(581)   oauth.signpost.exception.OAuthCommunicationException Communication with the service provider failed null
01-07 192624.691 WSystem.err(581)   at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java214)
01-07 192624.702 WSystem.err(581)   at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java69)

这里是代码sn-p

update = (Button) findViewById(R.id.update);
        status = (TextView) findViewById(R.id.status);
private  static  final  String CALLBACK_URI = OauthTwittertwitt;
private static final String REQUEST_TOKEN_URL = httpsapi.twitter.comoauthrequest_token;
private static final String ACCESS_TOKEN_URL = httpsapi.twitter.comoauthaccess_token;
private static final String AUTHORIZE_URL = httpsapi.twitter.comoauthauthorize;
private static CommonsHttpOAuthConsumer consumer;
private static DefaultOAuthProvider provider;
consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
provider = new DefaultOAuthProvider(REQUEST_TOKEN_URL,
                ACCESS_TOKEN_URL, AUTHORIZE_URL);
        update.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                 showMyDialog();

                try {
                    Log.d(in,check1);
                    String authUrl = provider.retrieveRequestToken(consumer,CALLBACK_URI);
                    Log.d(uri, authUrl);
                    startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(authUrl)));
                } catch (OAuthMessageSignerException e) {
                     TODO Auto-generated catch block
                    Log.d(signerg,1);
                    e.printStackTrace();
                } catch (OAuthNotAuthorizedException e) {
                     TODO Auto-generated catch block
                    Log.d(Notauths,2);
                    e.printStackTrace();
                } catch (OAuthExpectationFailedException e) {
                     TODO Auto-generated catch block
                    Log.d(authExceg,3);
                    e.printStackTrace();
                } catch (OAuthCommunicationException e) {
                     TODO Auto-generated catch block
                    Log.d(communication,4);
                    e.printStackTrace();
                }

            }

【问题讨论】:

    标签: android oauth twitter twitter-oauth


    【解决方案1】:

    这是由主线程上的网络异常引起的。尝试在线程上运行 oauth 代码:

              new Thread(new Runnable() {
                    public void run() {
                        String authUrl = provider.retrieveRequestToken(consumer,CALLBACK_URI);
                    }
                  }).start();
    

    【讨论】:

    • 我无法使用您的代码解决问题。我正在使用 avd 测试代码。可能存在一些设置问题。
    • 由于 HoneyComb android 在主 UI 线程上尝试联网时抛出了异常。 Oauth 通过网络执行一些调用,并且尚未更新以反映此错误。如果这是原因,您的代码在 android 2.3.3 上模拟时不应产生此错误。您也可以在错误堆栈跟踪中搜索,这将说明主线程错误中的网络。我认为您使用的是在 UI 线程上运行的最新版本的 android amd 是否正确?
    • 我没有在 UI 线程中运行它,是的,我使用的是最新版本的 android。
    猜你喜欢
    • 2012-02-06
    • 2014-06-14
    • 2011-12-01
    • 2021-02-27
    • 1970-01-01
    相关资源
    最近更新 更多