How to get profile picture in android using Twitter kit, - android

I'm trying to implement a simple application that uses Twitter kit. The problem is that i'm not able to get the profile picture.Any help would be appreciated.
Thanks

From the official doc:
You can obtain a user’s most recent profile image from GET users/show. Within the user object, you’ll find the profile_image_url
and profile_image_url_https fields. These fields will contain the
resized “normal” variant of the user’s uploaded image. This “normal”
variant is typically 48x48px.
By modifying the URL, you can retrieve other variant sizings such as
“bigger”, “mini”, and “original”.
Following the code:
TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient();
twitterApiClient.getAccountService().verifyCredentials(false, false, new Callback<User>() {
#Override
public void success(Result<User> userResult) {
String name = userResult.data.name;
String email = userResult.data.email;
// _normal (48x48px) | _bigger (73x73px) | _mini (24x24px)
String photoUrlNormalSize = userResult.data.profileImageUrl;
String photoUrlBiggerSize = userResult.data.profileImageUrl.replace("_normal", "_bigger");
String photoUrlMiniSize = userResult.data.profileImageUrl.replace("_normal", "_mini");
String photoUrlOriginalSize = userResult.data.profileImageUrl.replace("_normal", "");
}
#Override
public void failure(TwitterException exc) {
Log.d("TwitterKit", "Verify Credentials Failure", exc);
}
});
For further information refer to Twitter API Documentation | Profile Images and Banners

As of Nov 2016. This works.
There is a change in the implementation of verify credentials.
Call<User> user = TwitterCore.getInstance().getApiClient().getAccountService().verifyCredentials(false, false);
user.enqueue(new Callback<User>() {
#Override
public void success(Result<User> userResult) {
String name = userResult.data.name;
String email = userResult.data.email;
// _normal (48x48px) | _bigger (73x73px) | _mini (24x24px)
String photoUrlNormalSize = userResult.data.profileImageUrl;
String photoUrlBiggerSize = userResult.data.profileImageUrl.replace("_normal", "_bigger");
String photoUrlMiniSize = userResult.data.profileImageUrl.replace("_normal", "_mini");
String photoUrlOriginalSize = userResult.data.profileImageUrl.replace("_normal", "");
}
#Override
public void failure(TwitterException exc) {
Log.d("TwitterKit", "Verify Credentials Failure", exc);
}
});

Found the answer.
There is a callback which returns you the User object.
TwitterCore.getInstance().getApiClient().getAccountService().verifyCredentials(true, true, false).enqueue(new Callback<User>() {
#Override
public void success(Result<User> result) {
}
#Override
public void failure(TwitterException exception) {
}
});
On the success branch, you can get the User object by calling:
User user = userResult.data;
And from this object you can get all the information form the user. For the profile image:
String profileImage = user.profileImageUrl;

from gradle 2.0.0 and up use following method:
Call<User> userResult=Twitter.getApiClient(session).getAccountService().verifyCredentials(true,false);
userResult.enqueue(new Callback<User>() {
#Override
public void success(Result<User> result) {
User user = userResult.data;
String profileImage= user.profileImageUrl;
}
#Override
public void failure(TwitterException exception) {
}
});

Related

How to get user details from twitter API in android application

I am doing an android app with TwitterLogin Integration using Twitter Api. Here I am not using any twitter 4j and fabric . I am able to get Twitter user name but unable to get Email Id. searched more for this issue, but got nothing with twitter api.
I followed this twitterAPI to login
and this is my code
twitterLoginButton.setCallback(new Callback<TwitterSession>() {
#Override
public void success(Result<TwitterSession> result) {
System.out.println("=======twitterlogin success=======");
String username=result.data.getUserName();
getUsertwitteremail();
}
#Override
public void failure(TwitterException exception) {
System.out.println("=======twitterlogin failure==========");
}
});
please someone help me to get the details including email.
First of all make sure Request email addresses from users is checked for your Twitter app
Check out the code below and get the email
mTwitterAuthClient.authorize(this, new com.twitter.sdk.android.core.Callback<TwitterSession>() {
#Override
public void success(Result<TwitterSession> twitterSessionResult) {
TwitterSession session = TwitterCore.getInstance().getSessionManager().getActiveSession();
TwitterAuthToken authToken = session.getAuthToken();
String token = authToken.token;
String secret = authToken.secret;
long userId = session.getUserId();
String userNa = session.getUserName();
Log.d("twitter check", userNa + " " + secret);
mTwitterAuthClient.requestEmail(session, new Callback<String>() {
#Override
public void success(Result<String> result) {
Log.d("email", result.data);
// Do something with the result, which provides the email address
}
#Override
public void failure(TwitterException exception) {
// Do something on failure
}
});
}
#Override
public void failure(TwitterException e) {
e.printStackTrace();
}
});
please enable permissions for email access form your twitter app console.
from here
https://apps.twitter.com/app/14057942/permissions

Using facebook sdk 4.4.1. Want to get the number of invites sent by the app user using app invite dialog?

i want to calculate the number of person app user has invited from his friend list.
this is my code. in result i m getting null.
public void openDialogInvite(final Activity activity) {
String AppURl = "https://fb.me/659724450828700"; //Generated from //fb developers
//String previewImageUrl = " ";
sCallbackManager = CallbackManager.Factory.create();
if (AppInviteDialog.canShow()) {
AppInviteContent content = new AppInviteContent.Builder()
.setApplinkUrl(AppURl)
//.setPreviewImageUrl(previewImageUrl)
.build();
final AppInviteDialog appInviteDialog = new AppInviteDialog(activity);
appInviteDialog.registerCallback(sCallbackManager,
new FacebookCallback<AppInviteDialog.Result>() {
#Override
public void onSuccess(AppInviteDialog.Result result) {
Log.v("invitation", result.toString());
// setting parameters for request execution for both graph api request
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException e) {
Log.d("Invitation", "Error Occured");
}
});
appInviteDialog.show(content);
}
thanks in advance.
It is is not possible, to get the list of invited friends of a user until unless ur app is not a game on canvas.

Android how to get user's full name after authentication with Twitter

I have a Twitter Login created following this steps http://docs.fabric.io/android/twitter/authentication.html and It displays the Username Except the User's Full name.
This is my code below
loginButton.setCallback(new Callback<TwitterSession>() {
#Override
public void success(Result<TwitterSession> result) {
// Do something with result, which provides a TwitterSession for making API calls
TwitterSession session = Twitter.getSessionManager().getActiveSession();
TwitterAuthToken authToken = session.getAuthToken();
String token = authToken.token;
String secret = authToken.secret;
long userID = result.data.getUserId();
//This throws an Error Cannot find symbol showUser()
User user = twitter.showUser(userID);
//This throws error as well
String fullname= user.getName();
Log.d("login:", "twitter:success");
Log.d("username", result.data.getUserName());
Log.d("full name", fullname);
}
#Override
public void failure(TwitterException exception) {
// Do something on failure
}
});
Please how exactly can I get User's fullname?
Use TwitterApiClient and User like this:
Twitter
.getApiClient(session)
.getAccountService()
.verifyCredentials(true, false, new Callback<User>() {
#Override
public void failure(TwitterException e) {
}
#Override
public void success(Result<User> userResult) {
User user = userResult.data;
.... access User fields ....
}
});
You can use this function for getting user name:
public void processToken(String callbackUrl) {
mProgressDlg.setMessage("Finalizing ...");
mProgressDlg.show();
final String verifier = getVerifier(callbackUrl);
new Thread() {
#Override
public void run() {
int what = 1;
try {
mHttpOauthprovider.retrieveAccessToken(mHttpOauthConsumer,
verifier);
mAccessToken = new AccessToken(
mHttpOauthConsumer.getToken(),
mHttpOauthConsumer.getTokenSecret());
configureToken();
User user = mTwitter.verifyCredentials();
Log.i("user.getName()="+user.getName(), "786");
try{
firstName=user.getName().split(" ")[0];
lastName=user.getName().split(" ")[1];
}catch(Exception e){
e.printStackTrace();
firstName = user.getName();
lastName = "";
}
Log.i("firstName="+firstName+" lastName="+lastName, "786");
UserId=user.getId();
what = 0;
} catch (Exception e) {
e.printStackTrace();
}
mHandler.sendMessage(mHandler.obtainMessage(what, 2, 0));
}
}.start();
}
after login success, in your public void success(Result<TwitterSession> result) method, use this code.
#Override
public void success(Result<TwitterSession> result) {
TwitterSession sess = TwitterCore.getInstance().getSessionManager().getActiveSession();
TwitterApiClient apiClient = new TwitterApiClient(sess);
final Call<com.twitter.sdk.android.core.models.User> getUserCall = apiClient
.getAccountService()
.verifyCredentials(true, false,true);
new Thread(new Runnable() {
#Override
public void run() {
try {
com.twitter.sdk.android.core.models.User user = getUserCall.execute().body();
String realname = user.name;
long realid = user.id;
//similarly you can get user's other properties like location etc
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}

Get Twitter user email using Fabric SDK

I'm logging in with twitter using Fabric.
This is how I fetch the user data:
loginButton.setCallback(new Callback<TwitterSession>() {
#Override
public void success(Result<TwitterSession> result) {
// Do something with result, which provides a TwitterSession for making API calls
AccountService ac = Twitter.getApiClient(result.data).getAccountService();
ac.verifyCredentials(true, true, new Callback<com.twitter.sdk.android.core.models.User>() {
#Override
public void success(Result<com.twitter.sdk.android.core.models.User> result) {
String imageUrl = result.data.profileImageUrl;
String email = result.data.email;
String userName = result.data.name;
System.out.println(imageUrl);
System.out.println(email);
System.out.println(userName);
}
#Override
public void failure(TwitterException e) {
}
});
}
This is working fine, except that the email variable is null when I print to log. Is there an other way of fetching the user email?
-Here is the Solution!
twitauthobj.requestEmail(twitsessionobj,new Callback<String>() {
#Override
public void success(Result<String> stringResult) {
'You code here'
}
#Override
public void failure(TwitterException e) {
}
});
-Thanks let me inform if t doesnt work!
To bypass Twitter's useless request email activity and to fix a leak, I dug through the source code and pulled this out:
new Retrofit.Builder()
.client(getClient(sessionResult))
.baseUrl(new TwitterApi().getBaseHostUrl())
.addConverterFactory(getFactory())
.build()
.create(EmailService.class)
.getEmail()
.enqueue(new Callback<User>() {
#Override
public void success(Result<User> result) {
String email = result.data.email;
// Handle the result
if (email == null) {
TwitterProvider.this.failure(
new TwitterException("Your application may not have access to"
+ " email addresses or the user may not have an email address. To request"
+ " access, please visit https://support.twitter.com/forms/platform."));
} else if (email.equals("")) {
TwitterProvider.this.failure(
new TwitterException("This user does not have an email address."));
} else {
mCallbackObject.onSuccess(createIdpResponse(sessionResult.data, email));
}
}
#Override
public void failure(TwitterException exception) {
TwitterProvider.this.failure(exception);
}
});
private OkHttpClient getClient(Result<TwitterSession> sessionResult) {
return OkHttpClientHelper.getOkHttpClient(
sessionResult.data,
TwitterCore.getInstance().getAuthConfig(),
TwitterCore.getInstance().getSSLSocketFactory());
}
private GsonConverterFactory getFactory() {
return GsonConverterFactory.create(
new GsonBuilder()
.registerTypeAdapterFactory(new SafeListAdapter())
.registerTypeAdapterFactory(new SafeMapAdapter())
.registerTypeAdapter(BindingValues.class, new BindingValuesAdapter())
.create());
}
EmailService:
interface EmailService {
#GET("/1.1/account/verify_credentials.json?include_email=true?include_entities=true?skip_status=true")
Call<User> getEmail();
}

Android - Fabric.io Twitter REST API profile image / picture

Does anyone know if there is a way to pull a signed in users profile picture to be placed through the app, to maybe place it on the ActionBar as they navigate around?
hints, tips, examples, downloads all welcome :)
If you can help me, please assume I very little knowledge regarding anything outside basic Java!
Again, thanks people x
You can get a user's profile image by using /1.1/users/show.json. You can refer to REST API URLs for Twitter data.
By extending TwitterApiClient we can retrieve Twitter data from the URL.
class MyTwitterApiClient extends TwitterApiClient {
public MyTwitterApiClient(TwitterSession session) {
super(session);
}
public UsersService getUsersService() {
return getService(UsersService.class);
}
}
interface UsersService {
#GET("/1.1/users/show.json")
void show(#Query("user_id") Long userId,
#Query("screen_name") String screenName,
#Query("include_entities") Boolean includeEntities,
Callback<User> cb);
}
Next, get the UsersService and call its show method, passing in the defined query parameters. I defined the query parameters based on the ones that are documented.
new MyTwitterApiClient(session).getUsersService().show(12L, null, true,
new Callback<User>() {
#Override
public void success(Result<User> result) {
Log.d("twittercommunity", "user's profile url is "
+ result.data.profileImageUrlHttps);
}
#Override
public void failure(TwitterException exception) {
Log.d("twittercommunity", "exception is " + exception);
}
});
Courtesy: https://twittercommunity.com/t/android-get-user-profile-image/30579/2
This is how I got mine to work:
TwitterApiClient twitterApiClient = TwitterCore.getInstance().getApiClient();
twitterApiClient.getAccountService().verifyCredentials(false,false, new Callback<User>() {
#Override
public void success(Result<User> userResult) {
String name = userResult.data.name;
String profilebannerurl = userResult.data.profileBannerUrl;
String profileurl = userResult.data.profileImageUrl;
}
#Override
public void failure(TwitterException e) {
}
});
I have place this piece of code within my LoginButton callback method:
loginButton.setCallback(new Callback<TwitterSession>() {
#Override
public void success(Result<TwitterSession> result) { <insert here> }
I did it with a custom button and this is the code that is executed by it's onClick listener :
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_API_KEY, TWITTER_API_SECRET);
Fabric.with(activity, new Twitter(authConfig));
TwitterCore.getInstance().getApiClient().getAccountService().verifyCredentials(false, false, new com.twitter.sdk.android.core.Callback<User>() {
#Override
public void success(Result<User> result) {
Log.d(TAG, "Twitter log in success");
String userName = result.data.screenName;
int userId = result.data.id;
String pictureUrl = result.data.profileImageUrl;
String coverUrl = result.data.profileBannerUrl;
}
#Override
public void failure(TwitterException e) {
Log.d(TAG, "Twitter log in error : " + e.getMessage());
}
});
I should ask the user to authorize access to your app and log him in if he accepts.

Categories

Resources