问题
I've successfully integrated twitter's fabric api in my project A twitter login button
<com.twitter.sdk.android.core.identity.TwitterLoginButton
android:id="@+id/twitter_login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
A manifest permission
<meta-data
android:name="io.fabric.ApiKey"
android:value="myfabrickey" />
Initialization
//field declerations
private static final String TWITTER_KEY = "yourtwkey";
private static final String TWITTER_SECRET = "yoursecret";
//Inside oncreate
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Crashlytics(), new Twitter(authConfig));
loginButtonTwitter = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
loginButtonTwitter.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
// Do something with result, which provides a TwitterSession for making API calls
}
@Override
public void failure(TwitterException exception) {
// Do something on failure
}
});
//Inside onActivityResult
loginButtonTwitter.onActivityResult(requestCode, resultCode, data);
My only question is, how do i get the user data in a json format once i authenticate my account.
回答1:
Ok! It was quite simple. (was trying to do it during midnight so i guess half of my brain was asleep :P) Referenced from this link https://twittercommunity.com/t/android-get-user-profile-image/30579/6 In the user object, you'll get all the stuff.
TwitterSession session =
Twitter.getSessionManager().getActiveSession();
Twitter.getApiClient(session).getAccountService()
.verifyCredentials(true, false, new Callback<User>() {
@Override
public void success(Result<User> userResult) {
User user = userResult.data;
twitterImage = user.profileImageUrl;
}
@Override
public void failure(TwitterException e) {
}
});
回答2:
For those who getting error "verifyCredentials() in AccountService cannot be applied to .. " Please change the code like below
Call<User> userResult = Twitter.getApiClient(session).getAccountService().verifyCredentials(true,false);
userResult.enqueue(new Callback<User>() {
@Override
public void failure(TwitterException e) {
}
@Override
public void success(Result<User> userResult) {
User user = userResult.data;
String twitterImage = user.profileImageUrl;
try {
Log.d("imageurl", user.profileImageUrl);
Log.d("name", user.name);
Log.d("email",user.email);
Log.d("des", user.description);
Log.d("followers ", String.valueOf(user.followersCount));
Log.d("createdAt", user.createdAt);
} catch (Exception e) {
e.printStackTrace();
}
}
});
回答3:
Here is how to get user info after login through Twitter using Fabric.
Here is MainActivity.java class:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.twitter.sdk.android.Twitter;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterAuthConfig;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterLoginButton;
import com.twitter.sdk.android.core.models.User;
import io.fabric.sdk.android.Fabric;
public class MainActivity extends AppCompatActivity {
// Note: Your consumer key and secret should be obfuscated in your source code before shipping.
private static final String TWITTER_KEY = "your twitter api key";
private static final String TWITTER_SECRET = "your twitter secret key";
private TwitterLoginButton loginButton;
TextView textView;
TwitterSession session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig));
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.tv_username);
loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
loginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
session = result.data;
String username = session.getUserName();
Long userid = session.getUserId();
textView.setText("Hi " + username);
getUserData();
}
@Override
public void failure(TwitterException exception) {
Log.d("TwitterKit", "Login with Twitter failure", exception);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
loginButton.onActivityResult(requestCode, resultCode, data);
}
void getUserData() {
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;
String twitterImage = user.profileImageUrl;
try {
Log.d("imageurl", user.profileImageUrl);
Log.d("name", user.name);
Log.d("email",user.email);
Log.d("des", user.description);
Log.d("followers ", String.valueOf(user.followersCount));
Log.d("createdAt", user.createdAt);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
Here is MainActivity.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.sourcen.twitterlogin.MainActivity">
<TextView
android:id="@+id/tv_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="twitter username"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
<com.twitter.sdk.android.core.identity.TwitterLoginButton
android:id="@+id/twitter_login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
来源:https://stackoverflow.com/questions/30564935/how-to-get-user-data-through-twitters-fabric-api-android