我正在尝试登录Twitter,但我无法获取用户个人资料图片。
这是我的代码,使用自定义Twitter按钮。
TwitterCore.getInstance().logIn(LoginActivity.this, new com.twitter.sdk.android.core.Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
session = result.data;
authClient.requestEmail(session, new com.twitter.sdk.android.core.Callback<String>() {
@Override
public void success(Result<String> result) {
// Do something with the result, which provides the email address
String getUsername = session.getUserName();
userid = (int) session.getUserId();
int getprovider = (int) session.getId();
String photoUrlNormalSize =result.data.profileImageUrl;
Twitter twitter = new TwitterFactory().getInstance();
Log.i("getnamdfsge", "" + getUsername);
Log.i("succccesss", "" + result.data);
}
@Override
public void failure(TwitterException exception) {
// Do something on failure
Log.i("succccesss", "" + exception);
}
});
}
@Override
public void failure(TwitterException exception) {
Log.i("exeeeeee", "" + exception.toString());
}
});
错误发生在
String photoUrlNormalSize =result.data.profileImageUrl;
答案 0 :(得分:0)
以下是我设法获取Twitter用户数据的方法:
首先,我使用了Twitter4j
在onCreate中调用以下方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initTwitterConfigs();
initTwitter();
}
/ *从strings.xml * /
中读取twitter基本配置参数在strings.xml
中保存您的使用者密钥,密钥和回调网址private void initTwitterConfigs() {
consumerKey = getString(R.string.twitter_consumer_key);
consumerSecret = getString(R.string.twitter_consumer_secret);
callbackUrl = getString(R.string.twitter_callback);
oAuthVerifier = getString(R.string.twitter_oauth_verifier);
}
private void initTwitter() {
Uri uri = getIntent().getData();
if (uri != null && uri.toString().startsWith(callbackUrl)) {
String verifier = uri.getQueryParameter(oAuthVerifier);
try {
/* Getting oAuth authentication token */
twitter4j.auth.AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
/* save updated token */
saveTwitterInfo(accessToken);
} catch (Exception e) {
Log.e("TAG", "Login failed" + e.getMessage());
}
}
}
点击下面的Twitter登录按钮调用方法:
private void loginToTwitter() {
final ConfigurationBuilder builder = new ConfigurationBuilder();
builder.setOAuthConsumerKey(consumerKey);
builder.setOAuthConsumerSecret(consumerSecret);
final Configuration configuration = builder.build();
final TwitterFactory factory = new TwitterFactory(configuration);
twitter = factory.getInstance();
try {
requestToken = twitter.getOAuthRequestToken(callbackUrl);
final Intent intent = new Intent(this, WebViewActivity.class);
intent.putExtra(WebViewActivity.EXTRA_URL, requestToken.getAuthenticationURL());
startActivityForResult(intent, TWITTER_REQUEST_CODE);
} catch (TwitterException e) {
e.printStackTrace();
}
}
在on onctivityResult中,访问用户详细信息如下:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TWITTER_REQUEST_CODE) {
String verifier = data.getExtras().getString(oAuthVerifier);
try {
twitter4j.auth.AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
saveTwitterInfo(accessToken);
} catch (Exception e) {
Log.e("Twitter Login Failed", e.getMessage());
}
}
}
private void saveTwitterInfo(twitter4j.auth.AccessToken accessToken) {
long userID = accessToken.getUserId();
String uid = accessToken.getUserId() + "";
User user;
try {
user = twitter.showUser(userID);
String username = user.getName();
String fullName = user.getName();
String profileImage = user.getOriginalProfileImageURL();
String location = user.getLocation();
} catch (TwitterException e1) {
e1.printStackTrace();
}
}
希望这适合你。