0

我正在尝试使用 twitter 4j 加载 twitter 页面,但未加载 twitter 页面我已经注册了我的应用程序并在此处获取我的消费者密钥和消费者密钥这是我的代码

package a.a.a;


import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;


public class StatusListActivity extends ListActivity{


    private Otweet app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        app=(Otweet)getApplication();

        setContentView(R.layout.main);

    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if(!app.isAuthorized())
        {
        beginAuthorization();
        }
        else
        {

        }
    }
    public void beginAuthorization()
    {
        Intent intent=new Intent(StatusListActivity.this,AuthorizationActivity.class);
        startActivity(intent);
    }


}

这是授权活动

package a.a.a;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class AuthorizationActivity extends Activity{


    private Otweet app;
    WebView WV;
    Twitter twitter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        app=(Otweet)getApplication();
        setContentView(R.layout.authorization_view);
        WV=(WebView)findViewById(R.id.web_view);

    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        //twitter=new TwitterFactory().getInstance();
        //twitter.setOAuthConsumer("C6lqBhtUcC5f9wuauwoxag", "BttswrGyCxaeLgOZAdtfSqlbEg1uiw6xnuXD4y2rcjE");
        String authURL=app.BeginAuthorization();
        WV.loadUrl(authURL);
    }


}

这是 otweet 活动

public class Otweet extends Application {

    private OAuthHelper authhelper;
    private Twitter twitter;
    private RequestToken currentRequestToken;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        authhelper=new OAuthHelper(this);
        twitter=new TwitterFactory().getInstance();
        authhelper.configureOAuth(twitter);

    }
    public boolean isAuthorized()
    {
        return authhelper.hasAccessToken();
    }
    public String BeginAuthorization()
    {
        try
        {
            if(null==currentRequestToken)
            {
                currentRequestToken=twitter.getOAuthRequestToken();
            }
            return currentRequestToken.getAuthorizationURL();
        }
        catch (TwitterException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return null;
    }

}

函数开始授权总是返回 null。

4

1 回答 1

0

尝试更换

authhelper.configureOAuth(twitter)

twitter.setOAuthConsumer(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);

如果这不起作用,请尝试TwitterOAuthView

使用 TwitterOAuthView,您不必关心 OAuth 流程。打电话

view.start(CONSUMER_KEY, CONSUMER_SECRET, CALLBACK_URL, true, listener);

并通过如下定义的 TwitterOAuthView.Listener 接口接收结果。

void onSuccess(TwitterOAuthView view, AccessToken accessToken);
void onFailure(TwitterOAuthView view, TwitterOAuthView.Result result);
于 2012-07-18T18:42:50.840 回答