使用oAuth和TWITTER API ME 1.8库的Blackberry Twitter App

时间:2013-04-05 08:19:35

标签: blackberry twitter-oauth network-connection

我正在为BB建立一个Twitter应用程序,它连接到Twitter并代表用户发布帖子。使用TWITTER API ME 1.8 Library和oAuth身份验证方法。这是代码:

    class ShowAuthBrowser extends MainScreen implements OAuthDialogListener {
    BrowserField b = new BrowserField();

    public ShowAuthBrowser() {

        _authManager = new VerticalFieldManager(NO_VERTICAL_SCROLL
                | NO_VERTICAL_SCROLLBAR);
        _pinManager = new HorizontalFieldManager(NO_VERTICAL_SCROLL
                | NO_VERTICAL_SCROLLBAR);
        authButton = new ButtonField("OK");
        authPin = new TextField(Field.EDITABLE);
        _labelStatus = new LabelField("Your app is being authorized...");
        _authManager.add(_labelStatus);
        _authManager.add(b);

        _pinManager.add(authButton);
        _pinManager.add(authPin);

        pageWrapper = new BrowserFieldOAuthDialogWrapper(b, CONSUMER_KEY,
                CONSUMER_SECRET, CALLBACK_URL, this);

        pageWrapper.setOAuthListener(this);

        add(_pinManager);
        add(_authManager);

        authButton.setChangeListener( new FieldChangeListener( ) {
            public void fieldChanged( Field field, int context ) {
                if( field == authButton ) {
                       doAuth(authPin.getText());
                }
            }
        } );
    }

    public void doAuth(String pin) {
        try {
            if (pin == null) {
                pageWrapper.login();
            } else {
                this.deleteAll();
                add(b);
                pageWrapper.login(pin);
            }

        } catch (Exception e) {
            final String message = "Error loggin Twitter: "
                    + e.getMessage();
            Dialog.alert(message);

        }
    }

    public void onAccessDenied(String response) {

        System.out.println("Access denied! -> " + response);
        updateScreenLog("Acceso denegado! -> " + response);

    }

    public void onAuthorize(final Token token) {

        System.out.println("In authorize: "+token);
        final Token myToken = token;
        _tokenValue = StoreToken.fetch();
        _tokenValue.token = myToken.getToken();
        _tokenValue.secret = myToken.getSecret();
        _tokenValue.userId = myToken.getUserId();
        _tokenValue.username = myToken.getUsername();
        _tokenValue.save();

        UiApplication.getUiApplication().invokeLater(new Runnable() {

            public void run() {
                deleteAll();
                Credential c = new Credential(CONSUMER_KEY,
                        CONSUMER_SECRET, myToken);
                TweetToFriend tw = new TweetToFriend();
                String message = "Test Tweet...";
                boolean done = false;
                done = tw.doTweet(message, c);
                if (done == true) {
                    Dialog.alert("Tweet succusfully...");
                    close();
                }
            }
        });

    }

    public void onFail(String arg0, String arg1) {
        updateScreenLog("Error authenticating user! -> " + arg0 + ", "
                + arg1);
    }
}

private void updateScreenLog(final String message) {
    UiApplication.getUiApplication().invokeLater(new Runnable() {

        public void run() {
            _labelStatus.setText(message);
        }
    });
}

这里永远不会调用onAuthorize(),而是每次调用onFail()并抛出oAuth_io_exception,常规套接字错误。

任何人都可以帮助我。

感谢。

0 个答案:

没有答案