如何创建一个链接,如果用户已登录,则自动让用户关注某个Twitter用户,如果用户未登录,则将其发送到Twitter进行登录?我大约一个或两个月前找到了如何做到这一点,但现在找不到了。我想它是类似于一个链接或表单提交到像twitter.com/[user]/follow这样的东西。
我查看过API,但我需要用户在我的网站上进行身份验证,我不想处理这个问题。我只想让用户直接在Twitter上进行身份验证并且不必担心它。我找到的方法非常简单明了,我只想再次找到那个方法。
如何创建一个链接,如果用户已登录,则自动让用户关注某个Twitter用户,如果用户未登录,则将其发送到Twitter进行登录?我大约一个或两个月前找到了如何做到这一点,但现在找不到了。我想它是类似于一个链接或表单提交到像twitter.com/[user]/follow这样的东西。
我查看过API,但我需要用户在我的网站上进行身份验证,我不想处理这个问题。我只想让用户直接在Twitter上进行身份验证并且不必担心它。我找到的方法非常简单明了,我只想再次找到那个方法。
使用 Twitter 的网页意图(Web Intents)功能。
尽管你可以使用 关注按钮(Follow Button),但你也可以直接像下面这样将用户发送到Intent URL:
如何在我的Android应用程序中使用Twitter API来实现“关注”按钮
Android
代码片段:(我已将中文字符串转换为标准英语)
public class TimeLineActivity extends ListActivity {
private TwitterPreferenceManager tpm = new TwitterPreferenceManager(this);
private static final int FOLLOW = 1;
private static final CharSequence FOLLOW_LABEL = "Follow";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// timeline Obtaining process
String screenName = getIntent().getStringExtra("screen_name");
List<String> list = this.getTimeLine(screenName);
setListAdapter(new ArrayAdapter<String>(this, R.layout.timeline_item,list));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0, FOLLOW, 0, FOLLOW_LABEL);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case FOLLOW:
ConfigurationBuilder builder = new ConfigurationBuilder();
Configuration conf = builder.setOAuthAccessToken(tpm.getAccessToken())
.setOAuthAccessTokenSecret(tpm.getAccessTokenSercret())
.setOAuthConsumerKey(TwitterPreferenceManager.CONSUMER_KEY)
.setOAuthConsumerSecret(TwitterPreferenceManager.CONSUMER_SERCRET)
.setDebugEnabled(true)
.build();
Twitter twitter = new TwitterFactory(conf).getInstance();
try {
String screen_name = getIntent().getStringExtra("screen_name");
twitter.createFriendship(screen_name);
Toast.makeText(getApplicationContext(), "Was to follow.", Toast.LENGTH_LONG).show();
} catch (TwitterException e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
/**
* Get the time line for the specified user
*
* @param screenName
* @return
*/
private List<String> getTimeLine(String screenName) {
List<String> result = new ArrayList<String>();
Twitter twitter = new TwitterFactory().getInstance();
ResponseList<Status> userTimeline;
try {
userTimeline = twitter.getUserTimeline(screenName);
for (Status status : userTimeline) {
result.add(status.getText());
}
} catch (TwitterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
}
iPhone
http://www.chrismaddern.com/twitter-follow-button-for-ios-iphone-code/
这是实现的方法,如何操作
可以通过在Interface Builder中添加UIButton并将其类更改为FollowMeButton或使用自定义初始化程序在代码中创建FollowMeButton:
[self.view addSubview:[[FollowMeButton alloc] initWithTwitterAccount:@"chrismaddern" atOrigin:CGPointMake(205, 248) isSmallButton:YES]];
有两种尺寸模式可供选择,可以通过在初始化器中设置isSmallButton或稍后更改对象的isSmall属性来控制。