integrating twitter in my android application - android

i am integrating the twitter in to my android application. and it logins me successfully on twitter but i do not know how to get the tweets and statuses from that so i can show it in my application. here is my code.
String CONSUMER_KEY = "XXXXXXXXXXXXXXX";
String CONSUMER_SECRET = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";
try {
twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
requestToken = twitter.getOAuthRequestToken();
AccessToken accessToken = null;
String url = requestToken.getAuthorizationURL();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
}
catch (TwitterException e){
e.printStackTrace();
}
any help will be appreciated.

You can handle the login part, without the user introducing the pin if you use signpost, and once you have the token and the verifier, you can go on with twitter4j, creating the object twitter with TwitterFactory twitterfact=new TwitterFactory();
twitter = twitterfact.getOAuthAuthorizedInstance(consumerKey, consumerSecret,accessToken);
Now you can show the timeline with twitter.getFriendsTimeLine(). It's what I do, and it works fine. I can tweet, read tweets, send private messages... and the login part doesn't fail.

As soon as you have the twitter variable correctly populated (i.e. your code throws no Exception), you can use it to twitter.getHomeTImeline() etc.
Having said that, your code looks like it only does the first part of the OAuth procedure and that you still need to have code that sets the pin the user receives and then creates a fully OAuth authorized connection.
Have e.g. a look at LoginActivity of Zwitscher (v0.65 tag).
The method getPinButton() is basically what you have above. When the user comes back, he enters the pin in an EditText and clicks on the [setPinButton()][2] which provides the 2nd part of the OAuth stuff.
OAuth keyes and tokens are then stored in preferences for later use (you need them to create authenticated Twitter instances via the TwitterFactory (see e.g. TwitterHelper.getTwitter() on how to do this).

Related

How to open facebook app user-timeline page for a specific id from my android app?

I am trying to open facebook app for a specific user profile.
This is my code for opening the facebook intent:
try{
Toast.makeText(getActivity().getApplicationContext(),
facebook_id,
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://profile/" + facebook_id));
startActivity(intent);
}catch(Exception e){
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com/UserNamePage")));
}
This is how I received the user_id:
user.getId();
(The user is a GraphUser object from facebook sdk) Is this the correct way to get the user id and use it for openning the inetnt? Because this is not worked for me.
If you are using Graph API 2.x, then the user ID you have is an app-scoped user id, and there's no support for getting to a user profile from an app-scoped id in the Facebook app.

Sending Email from EditText in Android App

I'm having an issue similar to this: Adding text from edit text field into an email . I am able to do what this is doing, but how could I send the email straight from a submit button, instead of having it compose an email in a default email client. This should be able to be sent with as anonymous and not need to be sent by default from an installed email client.
You need to download JavaMail API:
Download: https://java.net/projects/javamail/pages/Home
You need an SMTP server, also username and password for auth.
String host="your smtp";
final String user="from email address";//change accordingly
final String password="frm email password";//change accordingly
String to="to email";//change accordingly
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth", "true");
javax.mail.Session session = javax.mail.Session.getDefaultInstance(props,new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(user,password);
}
});
//Compose the message
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(javax.mail.Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("javatpoint");
message.setText("This is simple program of sending email using JavaMail API");
//send the message
javax.mail.Transport.send(message);
System.out.println("message sent successfully...");
}
catch (MessagingException e)
{
e.printStackTrace();
}
There is no way to send an email silently, without either
letting the user know and accept it first (by using intents and an email provider)
or asking for the username and password before and using an email API as above (the user will implicitly give you the approval to send/receive emails by entering those values)
And that is a very good thing! There are too many security concerns otherwise. If you ever find a way, please post it as a bug report in android.
Workaround:
You need to use an email API such as JavaMail:
Sending Email in Android using JavaMail API without using the default/built-in app
There are some anonymous mailers that have APIs that you could use. But I would suggest you not do this. I actually agree with #DheeB 100%. I can't up vote yet.
Here is an example of one such service: http://api.temp-mail.ru/ It's in Russian but it can be translated. Again, I don't recommend but trying to answer your question.

Open Facebook page via Facebook app

How can I open Facebook page using insalled Facebook app? Exactly page, not profile, because fb://profile works fine.
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/page_id")));
Seems like fb://page isn't working, because it's just opens feed.
fb://page/{id} is the way to go:
final String url = "fb://page/" + facebookID
Intent facebookAppIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
facebookAppIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
startActivity(facebookAppIntent);
Are you sure you are using the ID number of your Facebook page and NOT the username?
What I mean is that if, for example, you want to open the Facebook page of Facebook itself (i.e. https://www.facebook.com/facebook) you have to use:
fb://page/20531316728
Since 20531316728 is the ID of the Facebook page (while facebook is the username).
If you don't know the ID of your Facebook page, you can retrieve it opening:
https://graph.facebook.com/{username}
Make Sure That You are Using the page id of your page correctly
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse("fb://page/YOUR PAGE ID"));
startActivity(intent);
i think u should use
https://www.facebook.com/
instead of fb://page/ or fb://profile or something.
I hope it's helpful!

Android-twitter4j Exception - no authentication challenges found

I have integrated Twitter into my application (using OAuth and twitter4j library) to update status on user's account on behalf of user. This works nice.
Before updating status, I check in Shared Preferences for stored AccessToken. If it is available, i.e. user is logged in, then update the status otherwise user is not logged in and hence display twitter page for user to login.
The problem is that if user has once logged in and hence I have AccessToken in Shared Preferences and now, if user revokes permission from application and I try to post status, then it gives this exception or error.
How do I come to know that user has revoked permission and thus I need to ask for authorization?
EDIT : I come to know how to handle the case of unuthorized access and I handled it. But still this exception is thrown.
It might sound weird but try to set your time/date update automatically. Based on my previous experience this was the problem. When i set this and after the phone time is synced, everything works fine.
Then try this :-
// ConfigurationBuilder
ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(twitterConsumerKey);
builder.setOAuthConsumerSecret(twitterConsumerSecret);
// Access Token from sharedPreference
String access_token = mSharedPreferences.getString(TwitterConstants.PREF_KEY_OAUTH_TOKEN, "");
// Access Token Secret from sharedPreference
String access_token_secret = mSharedPreferences.getString(TwitterConstants.PREF_KEY_OAUTH_SECRET, "");
AccessToken accessToken = new AccessToken(access_token, access_token_secret);
twitter = new TwitterFactory(builder.build()).getInstance(accessToken);
// Getting user details from twitter
// For now i am getting his name only
long userID = accessToken.getUserId();
User user = twitter.showUser(userID);
String username = user.getName() + " (" + user.getScreenName() + ")";
I solved the issue. The problem was that I was testing on what it does when user unauthorizes the application. And I unauthorized the application from twitter account which I used to create application at dev.twitter.com. Doing this invalidated my accesstoken which was stored in shared preferences and hence in my code when I fetch it from shared preferences and use it, I used to get exception.
The solution is to recreate an accesstoken in application created at dev.twitter.com. And in my code I added this condition :
catch (TwitterException e) {
if (e.getStatusCode() == -1) {
// This is the case when the developer who created the application has revoked the application permission.
// In this case we need to remove stored access token as it is no longer valid.
// Logging in again will fetch newer accesstoken and will solve the problem.
logoutFromTwitter();
loginToTwitter();
}
}

How can I link to the page of a specific Facebook newsfeed post (in the application or browser)?

In my Android app, I log in using the Facebook SDK and display posts from the users' newsfeed. I want to give them the option to comment or like a post, but I don't want to implement that. So, my solution is to allow the user to click on the post, which will then load the post page in either the official Facebook App, or just the mobile facebook website. Either way is fine for me.
I googled around and was not able to find a suitable way to deliver an intent to launch the Facebook app to a specific post page. So, I decided to try to launch the browser and navigate to the mobile Facebook website to the specific post. I build the url string using information about the current user, the id of the post, and the id of the friend it belongs to. This url is exactly the same url I see when I browse to the page in my browser from my newsfeed. Like so:
String url = "http://m.facebook.com/#!/story.php?story_fbid=" + postId + "&id=" + friendId + "&_user=" + FBHelper.getCurrentUserId();
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
However, when the browser loads, I get a page that says "Sorry, something went wrong. We're working on getting this fixed as soon as we can." I have tried both touch.facebook.com and m.facebook.com.
I am looking for any solution that will either allow me to open the mobile site to the chosen post, or to launch the Facebook app to the chosen post activity.
I just figured this out. It turns out that the post id that I got from the json result of a newsfeed request to the graph api has extra information. The post id that I got from the newsfeed result is in the form "friendid_postid." In the link, however, the "story_fbid" tag should be set to just the postid portion. Then it works!
This is the code I used:
String postId = idTextView.getText().toString();
postId = postId.substring(postId.indexOf("_") + 1, postId.length());
String friendId = friendIdTextView.getText().toString();
String url = "http://m.facebook.com/#!/story.php?story_fbid=" + postId + "&id=" + friendId + "&_user=" + FBHelper.getCurrentUserId();
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);

Categories

Resources