Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 6
Registered: ‎06-19-2012
My Device: 9800
My Carrier: no

twitter post problem

hi,

i facing some problem in twitter post.

i using blackberry plugins-1.3. OS 6.0 & jdk 1.5.

jar file: twitter_api_me-1.8.3jar, kxml-min-2.3.0jar

i have using this code:

 

package mypackage;
import impl.rim.com.twitterapime.xauth.ui.BrowserContentManagerOAuthDialogWrapper;
import net.rim.device.api.browser.field.BrowserContentManager;
import net.rim.device.api.browser.field.RenderingOptions;
import net.rim.device.api.ui.container.MainScreen;
import com.twitterapime.rest.Credential;
import com.twitterapime.rest.TweetER;
import com.twitterapime.rest.UserAccountManager;
import com.twitterapime.search.Tweet;
import com.twitterapime.xauth.Token;
import com.twitterapime.xauth.ui.OAuthDialogListener;
import com.twitterapime.xauth.ui.OAuthDialogWrapper;
public class screen2 extends MainScreen implements OAuthDialogListener{
    private final String CONSUMER_KEY = "< >";
    private final String CONSUMER_SECRET ="< >";
    private final String CALLBACK_URL = "";
    public screen2()
    {
        System.out.println("const call");
        BrowserContentManager browserMngr = new BrowserContentManager(0);
        RenderingOptions rendOptions = browserMngr.getRenderingSession().getRenderingOptions();
        rendOptions.setProperty(
            RenderingOptions.CORE_OPTIONS_GUID,
            RenderingOptions.SHOW_IMAGES_IN_HTML,
            false);
        add(browserMngr);
        OAuthDialogWrapper pageWrapper =
            new BrowserContentManagerOAuthDialogWrapper(browserMngr);
        pageWrapper.setConsumerKey(CONSUMER_KEY);
        pageWrapper.setConsumerSecret(CONSUMER_SECRET);
        pageWrapper.setCallbackUrl(CALLBACK_URL);
        pageWrapper.setOAuthListener(this);
        System.out.println("key vals"+CONSUMER_KEY+"secontd vale "+CONSUMER_SECRET);
        pageWrapper.login();        
    }
    
    public void onAuthorize(Token token) {    
        //System.out.println("key vals in auth"+CONSUMER_KEY+"secontd vale "+CONSUMER_SECRET);
        Credential c = new Credential(CONSUMER_KEY,    CONSUMER_SECRET, token);
        UserAccountManager uam = UserAccountManager.getInstance(c);
        try {
            if (uam.verifyCredential()) {
                System.out.println("uamverifyval"+uam.verifyCredential());
                TweetER.getInstance(uam).post(new Tweet("test: " + System.currentTimeMillis()));
                System.out.println("Tweet posted!");
            }
        } catch (Exception e) {
            System.out.println("Error by posting tweet.");
        }
    }
    public void onAccessDenied(String message) {
        System.out.println("Access denied!");
        }
    public void onFail(String error, String message) {
        System.out.println("Error by authenticating user!");
        }
}

problem:

when i use this code login form open successfully & after login got the pin number but could not found post on twitter account .

please resolve my problem as soon. i'll wait for you revert..

 

 

Developer
Posts: 209
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: twitter post problem

Hi Ankit

what are you using in CALLBACK_URL ?

if CALLBACK_URL is blank, then use any url there. your you post will be success.

Thanks
New Contributor
Posts: 6
Registered: ‎06-19-2012
My Device: 9800
My Carrier: no

Re: twitter post problem

i have used null but problem is same.

and i also use companie's url what i got at registration like: "http://www.appstudioz.com"

 

 

thansk for your reply

Developer
Posts: 209
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: twitter post problem

After putting this url, still you are facing issue?

Thanks
pawan
New Contributor
Posts: 6
Registered: ‎06-19-2012
My Device: 9800
My Carrier: no

Re: twitter post problem

if i use url like: "abc.com" or "' both condition i cound not found post on twitter.

Developer
Posts: 209
Registered: ‎04-30-2011
My Device: BlackBerry Z10
My Carrier: Airtel

Re: twitter post problem

New Contributor
Posts: 6
Registered: ‎06-19-2012
My Device: 9800
My Carrier: no

Re: twitter post problem

Hi sir,

 

i use your code but problem is same. i modified your code like:

 

package mypackage;
import net.rim.device.api.ui.UiApplication;
public class StartApp extends UiApplication {
    public StartApp() {
        this.pushScreen(new TwitterScreen());
    }

    public static void main(String[] args) {
        StartApp myApp = new StartApp();
        myApp.enterEventDispatcher();
    }
}

 

 

package mypackage;
import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
import com.twitterapime.rest.Credential;
import com.twitterapime.rest.TweetER;
import com.twitterapime.rest.UserAccountManager;
import com.twitterapime.search.Tweet;
import com.twitterapime.xauth.Token;
import com.twitterapime.xauth.ui.OAuthDialogListener;
import com.twitterapime.xauth.ui.OAuthDialogWrapper;
public class TwitterScreen extends MainScreen  {

    private final String CONSUMER_KEY ="IL5puJUU9FXqzeN32kWbTg";
    private final String CONSUMER_SECRET = "k7gFSUXAEH0Glod9nsqyWjhxH0Q06xbGk9K9apecI";
    private final String CALLBACK_URL ="";
    private LabelField _labelStutus;
    private OAuthDialogWrapper pageWrapper = null;
    public StoreToken _tokenValue;
    ShowAuthBrowser showAuthBrowserScreen;

    public TwitterScreen()
    {
        setTitle("Twitter Integration using OAuth");

        showAuthBrowserScreen = new ShowAuthBrowser();

        add ( new ButtonField("Tweet" ) {
            protected boolean navigationUnclick(int status, int time) {

                _tokenValue = StoreToken.fetch();

                if(_tokenValue.token.equalsIgnoreCase("nothing"))
                {
                    showAuthBrowserScreen.doAuth( null );
                    UiApplication.getUiApplication().pushScreen(showAuthBrowserScreen);
                }
                else
                {
                    /************************************************************/
                    System.out.println("else part");
/*                    Token t = new Token(_tokenValue.token, _tokenValue.secret, _tokenValue.userId, _tokenValue.username);
                    Credential c = new Credential(CONSUMER_KEY,    CONSUMER_SECRET, t);
                    TweetToFriend tw = new TweetToFriend();
                    String message="Hey, I did implment follow & unfollow concept too.... hurrahhhhhhhh";
                    boolean done=false;
                    done=tw.doTweet(message, c);
                    if(done == true)
                    {
                        Dialog.alert("Tweet send successfully");
                    }*/
                    /*boolean done=false;
                    String _friendName = "Pawan Kumar jha";

                    try
                    {
                        FriendshipManagement fm = new FriendshipManagement();
                        done=fm.doFollowFriend(c, _friendName);
                        if(done == true)
                        {
                            Dialog.alert("Now you are  Following to " +_friendName);
                        }

                    }
                    catch (Exception e) {
                        // TODO: handle exception
                        Dialog.alert("Exception" + e.getMessage());
                    }*/
                    /************************************************************/
 
                }
                return true;
            }
        });
    }
    class ShowAuthBrowser extends MainScreen implements OAuthDialogListener
    {
        BrowserField b = new BrowserField();

        public ShowAuthBrowser()
        {    
            _labelStutus = new LabelField("Your app is being authorize." );
            add(_labelStutus );
            add(b);
            pageWrapper = new BrowserFieldOAuthDialogWrapper(b,CONSUMER_KEY,CONSUMER_SECRET,CALLBACK_URL,this);
            pageWrapper.setOAuthListener(this);

        }
        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("authorize call1");
            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();
            System.out.println("authorize call2");
            UiApplication.getUiApplication().invokeLater( new Runnable() {

                public void run() {
                    deleteAll();
                    System.out.println("authorize call3");
                    //Credential c = new Credential(CONSUMER_KEY,    CONSUMER_SECRET, myToken);
                    Token t = new Token(_tokenValue.token, _tokenValue.secret, _tokenValue.userId, _tokenValue.username);
                    Credential c = new Credential(CONSUMER_KEY,    CONSUMER_SECRET, t);
                    TweetToFriend tw = new TweetToFriend();
                    String message="Hey, I did implment follow & unfollow concept too.... hurrahhhhhhhh";
                    System.out.println("authorize call4");
                    boolean done=false;
                    done=tw.doTweet(message, c);
                    if(done == true)
                    {
                        Dialog.alert("Tweet send successfully");
                    }
                    /*UserAccountManager uam = UserAccountManager.getInstance(c);
                    try {
                        if (uam.verifyCredential()) {
                            System.out.println("uamverifyval"+uam.verifyCredential());
                            TweetER.getInstance(uam).post(new Tweet("test: " + System.currentTimeMillis()));
                            System.out.println("Tweet posted!");
                        }
                    } catch (Exception e) {
                        System.out.println("Error by posting tweet.");
                    }*/
                    //TweetToFriend tw = new TweetToFriend();
                    //String message="";
                    //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() {
                _labelStutus.setText( message );                
            }
        });
    }
   
}

 

package mypackage;
import net.rim.device.api.browser.field2.BrowserField;
import net.rim.device.api.browser.field2.BrowserFieldListener;

import org.w3c.dom.Document;

import com.twitterapime.xauth.ui.OAuthDialogListener;
import com.twitterapime.xauth.ui.OAuthDialogWrapper;
 
public final class BrowserFieldOAuthDialogWrapper extends OAuthDialogWrapper {
 
    private BrowserField browserField;
    
    
    public BrowserFieldOAuthDialogWrapper(BrowserField browserField,
        String consumerKey, String consumerSecret, String callbackUrl,
        OAuthDialogListener oauthListener) {
        super(consumerKey, consumerSecret, callbackUrl, oauthListener);
        //
        if (browserField == null) {
            throw new IllegalArgumentException(
                "BrowserField must not be null.");
        }
        //
        this.browserField = browserField;
        this.browserField.addListener(new BrowserFieldListenerOAuth());
    }
    
    
    public BrowserFieldOAuthDialogWrapper(BrowserField browserField) {
        this(browserField, null, null, null, null);
    }
 
    protected void loadUrl(String url) {
        browserField.requestContent(url);
    }
 
    protected void loadHTML(String htmlContent) {
        browserField.displayContent(htmlContent, callbackUrl);
    }
 
    private class BrowserFieldListenerOAuth extends BrowserFieldListener {
    
        public void documentLoaded(BrowserField browserField, Document document)
            throws Exception {
            trackUrl(browserField.getDocumentUrl());
        }
    }
}

 

package mypackage;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.util.Persistable;

public class StoreToken implements Persistable{
private static PersistentObject storage;
private static final long PERSISTENCE_UNIQE_ID= 0xc70d21c86bf899f2L;
 
 public String token, secret, userId, username;
protected  StoreToken()
{
    token = "nothing";
    secret = "nothing";
    userId = "nothing";
    username = "nothing";
}

public static StoreToken fetch()
{
    Object ob = storage.getContents();
    StoreToken saved_data = (StoreToken)ob;
    return saved_data;
}

public void save()
{
storage.setContents(this);
storage.commit();
}
static {
    storage = PersistentStore.getPersistentObject(PERSISTENCE_UNIQE_ID);
    synchronized (storage) {
        if(storage.getContents()==null)
        {
            storage.setContents(new StoreToken());
            storage.commit();
        }
    }
}
}

 

i got pin number if i blank the callbackurl. and onauthorize method could not call that's why post could not be done.

pls check this code & find the resolution. is it possible to post on twitter with this way or not. if  you have another way so pls tell me.

New Contributor
Posts: 6
Registered: ‎06-19-2012
My Device: 9800
My Carrier: no

Re: twitter post problem

hello sir,

 

pls revert back of my status. i still unable to post on twitter. that code is working on my previous code like (login page open, after login getting pin number but post is still pending)

Highlighted
New Contributor
Posts: 6
Registered: ‎06-19-2012
My Device: 9800
My Carrier: no

Re: twitter post problem

Anybody is there to resolve my problem?