Friendships/create

跳转到: 导航, 搜索

目录

friendships/create

关注一个用户。成功则返回关注人的资料,目前的最多关注2000人,失败则返回一条字符串的说明。如果已经关注了此人,则返回http 403的状态。关注不存在的ID将返回400。

URL

http://api.t.sina.com.cn/friendships/create/id.format

格式

xml, json

HTTP请求方式

POST

是否需要登录

True

请求数限制

True

关于请求数限制,参见接口访问权限说明

请求参数

必选参数(至少选一个)

  • id: 要关注的用户UID或微博昵称
示例: http://api.t.sina.com.cn/friendships/create/12345.json or http://api.t.sina.com.cn/friendships/create/bob.xml
  • user_id: 要关注的用户UID,主要是用在区分用户UID跟微博昵称一样,产生歧义的时候。
示例: http://api.t.sina.com.cn/friendships/create.xml?user_id=1401881
  • screen_name: 要关注的微博昵称,主要是用在区分用户UID跟微博昵称一样,产生歧义的时候。
示例: http://api.t.sina.com.cn/friendships/create.xml?screen_name=101010
  • follow. 可选参数。暂不支持。

使用说明

  • 此接口有更新上限,越权将返回http 403 错误
  • 关注不存在的ID返回400

返回结果

XML

<?xml version="1.0" encoding="UTF-8"?>
<user>
  <id>1266321801</id>
  <screen_name>姚晨</screen_name>
  <name>姚晨</name>
  <province>11</province>
  <city>5</city>
  <location>北京 朝阳区</location>
  <description>一颗很逊的卤蛋。</description>
  <url>http://blog.sina.com.cn/yaochen</url>
  <profile_image_url>http://tp2.sinaimg.cn/1266321801/50/1283203855</profile_image_url>
  <domain>yaochen</domain>
  <gender>f</gender>
  <followers_count>2786967</followers_count>
  <friends_count>561</friends_count>
  <statuses_count>2148</statuses_count>
  <favourites_count>10</favourites_count>
  <created_at>Fri Aug 28 00:00:00 +0800 2009</created_at>
  <following>false</following>
  <verified>true</verified>
  <allow_all_act_msg>false</allow_all_act_msg>
  <geo_enabled>false</geo_enabled>
  <status>
    <created_at>Fri Sep 10 09:47:00 +0800 2010</created_at>
    <id>2465393025</id>
    <text>愿我永远不要失去给他人带来快乐的能力,因为失去它,自己也不可能再拥有快乐。</text>
    <source>
      <a href="http://news.sina.com.cn/wap/wbclient.html#3">iPhone客户端</a>
    </source>
    <favorited>false</favorited>
    <truncated>false</truncated>
    <geo/>
    <in_reply_to_status_id/>
    <in_reply_to_user_id/>
    <in_reply_to_screen_name/>
    <retweeted_status>
      <created_at>Fri Sep 10 08:00:03 +0800 2010</created_at>
      <id>2463041144</id>
      <text>许多人知道如何享乐,却不知道自己从何时起已不再向别人提供欢乐。(BY阿瑟·赫尔普斯)早安,各位。</text>
      <source>
        <a href="http://t.sina.com.cn">新浪微博</a>
      </source>
      <favorited>false</favorited>
      <truncated>false</truncated>
      <geo/>
      <in_reply_to_status_id/>
      <in_reply_to_user_id/>
      <in_reply_to_screen_name/>
      <thumbnail_pic>http://ss16.sinaimg.cn/thumbnail/62914aabt8fdc6a2f6c0f&690</thumbnail_pic>
      <bmiddle_pic>http://ss16.sinaimg.cn/bmiddle/62914aabt8fdc6a2f6c0f&690</bmiddle_pic>
      <original_pic>http://ss16.sinaimg.cn/orignal/62914aabt8fdc6a2f6c0f&690</original_pic>
      <user>
        <id>1653689003</id>
        <screen_name>新周刊</screen_name>
        <name>新周刊</name>
        <province>44</province>
        <city>1</city>
        <location>广东 广州</location>
        <description>《新周刊》,中国最新锐的时事生活周刊:观点供应商,视觉开发商,资讯整合商,传媒运营商。</description>
        <url>http://blog.sina.com.cn/xinzhoukan</url>
        <profile_image_url>http://tp4.sinaimg.cn/1653689003/50/1283204286</profile_image_url>
        <domain>neweekly</domain>
        <gender>m</gender>
        <followers_count>559667</followers_count>
        <friends_count>203</friends_count>
        <statuses_count>5748</statuses_count>
        <favourites_count>10</favourites_count>
        <created_at>Wed Oct 14 00:00:00 +0800 2009</created_at>
        <following>false</following>
        <verified>true</verified>
        <allow_all_act_msg>false</allow_all_act_msg>
        <geo_enabled>false</geo_enabled>
      </user>
    </retweeted_status>
  </status>
</user>

JSON

{
   "id":1266321801,
   "screen_name":"姚晨",
   "name":"姚晨",
   "province":"11",
   "city":"5",
   "location":"北京 朝阳区",
   "description":"一颗很逊的卤蛋。",
   "url":"http://blog.sina.com.cn/yaochen",
   "profile_image_url":"http://tp2.sinaimg.cn/1266321801/50/1283203855",
   "domain":"yaochen",
   "gender":"f",
   "followers_count":2787035,
   "friends_count":561,
   "statuses_count":2148,
   "favourites_count":10,
   "created_at":"Fri Aug 28 00:00:00 +0800 2009",
   "following":false,
   "allow_all_act_msg":false,
   "geo_enabled":false,
   "verified":true,
   "status":{
      "created_at":"Fri Sep 10 09:47:00 +0800 2010",
      "id":2465393025,
      "text":"愿我永远不要失去给他人带来快乐的能力,因为失去它,自己也不可能再拥有快乐。",
      "source":"<a href=\"http://news.sina.com.cn/wap/wbclient.html#3\" rel=\"nofollow\">iPhone客户端</a>",
      "favorited":false,
      "truncated":false,
      "in_reply_to_status_id":"",
      "in_reply_to_user_id":"",
      "in_reply_to_screen_name":"",
      "geo":null,
      "retweeted_status":{
         "created_at":"Fri Sep 10 08:00:03 +0800 2010",
         "id":2463041144,
         "text":"许多人知道如何享乐,却不知道自己从何时起已不再向别人提供欢乐。(BY阿瑟·赫尔普斯)早安,各位。",
         "source":"<a href=\"http://t.sina.com.cn\" rel=\"nofollow\">新浪微博</a>",
         "favorited":false,
         "truncated":false,
         "in_reply_to_status_id":"",
         "in_reply_to_user_id":"",
         "in_reply_to_screen_name":"",
         "thumbnail_pic":"http://ss16.sinaimg.cn/thumbnail/62914aabt8fdc6a2f6c0f&690",
         "bmiddle_pic":"http://ss16.sinaimg.cn/bmiddle/62914aabt8fdc6a2f6c0f&690",
         "original_pic":"http://ss16.sinaimg.cn/orignal/62914aabt8fdc6a2f6c0f&690",
         "geo":null,
         "user":{
            "id":1653689003,
            "screen_name":"新周刊",
            "name":"新周刊",
            "province":"44",
            "city":"1",
            "location":"广东 广州",
            "description":"《新周刊》,中国最新锐的时事生活周刊:观点供应商,视觉开发商,资讯整合商,传媒运营商。",
            "url":"http://blog.sina.com.cn/xinzhoukan",
            "profile_image_url":"http://tp4.sinaimg.cn/1653689003/50/1283204286",
            "domain":"neweekly",
            "gender":"m",
            "followers_count":559667,
            "friends_count":203,
            "statuses_count":5748,
            "favourites_count":10,
            "created_at":"Wed Oct 14 00:00:00 +0800 2009",
            "following":false,
            "allow_all_act_msg":false,
            "geo_enabled":false,
            "verified":true
         }
      }
   }
}

字段说明 - user

  • id: 用户UID
  • screen_name: 微博昵称
  • name: 友好显示名称,同微博昵称
  • province: 省份编码(参考省份编码表)
  • city: 城市编码(参考城市编码表)
  • location:地址
  • description: 个人描述
  • url: 用户博客地址
  • profile_image_url: 自定义图像
  • domain: 用户个性化URL
  • gender: 性别,m--男,f--女,n--未知
  • followers_count: 粉丝数
  • friends_count: 关注数
  • statuses_count: 微博数
  • favourites_count: 收藏数
  • created_at: 创建时间
  • following: 是否已关注(此特性暂不支持)
  • verified: 加V标示,是否微博认证用户

字段说明 - status

  • created_at: 创建时间
  • id: 微博ID
  • text: 微博信息内容
  • source: 微博来源
  • favorited: 是否已收藏
  • truncated: 是否被截断
  • in_reply_to_status_id: 回复ID
  • in_reply_to_user_id: 回复人UID
  • in_reply_to_screen_name: 回复人昵称
  • thumbnail_pic: 缩略图
  • bmiddle_pic: 中型图片
  • original_pic:原始图片
  • user: 作者信息
  • retweeted_status: 转发的博文,内容为status,如果不是转发,则没有此字段

使用示例

  • xml:

curl -u user:password -d "" http://api.t.sina.com.cn/friendships/create/timyang.xml

  • json:

curl -u user:password -d "" http://api.t.sina.com.cn/friendships/create/timyang.json

Java示例

请从 微博SDK开发包下载 下载Java SDK
代码示例如下:

package weibo4j.examples;

import weibo4j.Weibo;
import weibo4j.WeiboException;

public class CreateFriendship {

	/**
	 * 关注某用户 
	 * @param args
	 */
	public static void main(String[] args) {
		System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
		System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
		try {
			User user = getWeibo(true,args).createFriendship(args[2]);//args[2]:关注用户的id
			System.out.println(user.toString());
		} catch (WeiboException e) {
			e.printStackTrace();
		}
	}
	
	private static Weibo getWeibo(boolean isOauth,String[] args) {
		Weibo weibo = new Weibo();
		if(isOauth) {//oauth验证方式 args[0]:访问的token;args[1]:访问的密匙
			weibo.setToken(args[0], args[1]);
		}else {//用户登录方式
    		weibo.setUserId(args[0]);//用户名/ID
    		weibo.setPassword(args[1]);//密码
	 	}
	 	return weibo;
	}
}

PHP示例

请从 微博SDK开发包下载 处下载PHP SDK(支持OAuth验证之版本)
代码示例如下:

//friendships/create
//关注某用户
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
//关注用户的id
$u_id = "U_ID";
$msg = $c->follow($u_id);
if ($msg === false || $msg === null){
	echo "Error occured";
	return false;
}
if (isset($msg['error_code']) && isset($msg['error'])){
	echo ('Error_code: '.$msg['error_code'].';  Error: '.$msg['error'] );
	return false;
}
if (isset($msg['screen_name'])){
	echo('New Friend:'.$msg['screen_name']);
}