一切看起来都不错,但函数retrieveRequestToken() 抛出“OAuthCommunicationException”。例外

发布于 2024-12-25 13:32:17 字数 2678 浏览 0 评论 0原文

我正在尝试构建一个 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)

这是代码片段

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();
                }

            }

I am trying to build an android app so that it tweets in twitter.
I am trying to get uri using the function retrieveRequestToken(consumer,CALLBACK_URI) but it throws 'OAuthCommunicationException' exception.
I gave the internet uses permission for the application( i have checked it ).I have registerd the app in twitter giving read,write and direct messages.

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)

Here is the code snippet

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();
                }

            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

◇流星雨 2025-01-01 13:32:17

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

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

This is caused by a network on main thread exception. Try running the oauth code on a thread:

          new Thread(new Runnable() {
                public void run() {
                    String authUrl = provider.retrieveRequestToken(consumer,CALLBACK_URI);
                }
              }).start();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文