Direct messages/new/en

跳转到: 导航, 搜索

目录

direct_messages/new

Send a direct messae. Request must be handed in POST method. It returns the direct message containing sender and recipient’s information if sending is OK.

URL

http://api.t.sina.com.cn/direct_messages/new.(json%7Cxml)

Supported Formats

XML/JSON

HTTP Request Method

POST

Requires Authentication

true
See the Authorization Mechanism Statement for authorization details

Requests Count Limitation

true
See the Interface Access Rights Statement for the Request Count Limitaiton details.

Request Parameters

  Requires Type and Range Description
source true string AppKey for the application to identify it. ( This parameter is not needed when using OAuth)
id true int64/string Recipient’s user ID(int64) or nickname(string)
text true string Message content to be sent. It has to be encoded by URLEncode, and has 300 characters limitation.
user_id false int64 Recipient’s user ID,when user ID is easy to confuse with nickname, it is suggest to use this parameter.
screen_name false string Recipient’s nickname, when user ID is easy to confuse with nickname, it is suggest to use this parameter.

Notes

  • In order to avoid repeated submission, if the direct message to be sent is the same as the latest direct message, it returns 400 error.
  • Recipient must be sender’s follower. Otherwise the direct message can not be sent successfully , the system will return 400 error, waring: 40017:Error: can't send direct message to user who is not your follower!

Example Request

XML
curl -u "username:password" -d "id=11051&text=Congratulations!" "http://api.t.sina.com.cn/direct_messages/new.xml?source=appkey"
JSON
curl -u "username:password" -d "id=11051&text=Congratulations!" "http://api.t.sina.com.cn/direct_messages/new.json?source=appkey"

Response

XML Example

<?xml version="1.0" encoding="UTF-8"?>
<direct_message>
  <created_at>Mon Dec 06 11:19:09 +0800 2010</created_at>
  <id>722704713</id>
  <text>Congratulations!</text>
  <sender_id>1658122963</sender_id>
  <recipient_id>11051</recipient_id>
  <sender_screen_name>zhangwei217245</sender_screen_name>
  <recipient_screen_name>微博开放平台</recipient_screen_name>
  <sender>
    <id>1658122963</id>
    <screen_name>zhangwei217245</screen_name>
    <name>zhangwei217245</name>
    <province>11</province>
    <city>5</city>
    <location>北京 朝阳区</location>
    <description>ABCDE</description>
    <url/>
    <profile_image_url>http://tp4.sinaimg.cn/1658122963/50/1282754213</profile_image_url>
    <domain>zhangwei217245</domain>
    <gender>m</gender>
    <followers_count>24</followers_count>
    <friends_count>44</friends_count>
    <statuses_count>118</statuses_count>
    <favourites_count>3</favourites_count>
    <created_at>Tue Nov 03 00:00:00 +0800 2009</created_at>
    <following>false</following>
    <verified>false</verified>
    <allow_all_act_msg>false</allow_all_act_msg>
    <geo_enabled>true</geo_enabled>
    <status>
      <created_at>Sat Dec 04 23:35:20 +0800 2010</created_at>
      <id>4087921127</id>
      <text>动物丛书的由来。。。http://sinaurl.cn/hbVjvr</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/>
      <annotations/>
    </status>
  </sender>
  <recipient>
    <id>11051</id>
    <screen_name>微博开放平台</screen_name>
    <name>微博开放平台</name>
    <province>11</province>
    <city>8</city>
    <location>北京 海淀区</location>
    <description>新浪微博开放平台市场推广官方账号,如有技术问题,请@微博API或者发私信给微博API</description>
    <url>http://open.t.sina.com.cn/</url>
    <profile_image_url>http://tp4.sinaimg.cn/11051/50/1280283165/1</profile_image_url>
    <domain>openapi</domain>
    <gender>m</gender>
    <followers_count>13366</followers_count>
    <friends_count>5</friends_count>
    <statuses_count>158</statuses_count>
    <favourites_count>0</favourites_count>
    <created_at>Wed Jan 20 00:00:00 +0800 2010</created_at>
    <following>false</following>
    <verified>true</verified>
    <allow_all_act_msg>true</allow_all_act_msg>
    <geo_enabled>true</geo_enabled>
    <status>
      <created_at>Mon Nov 29 16:08:43 +0800 2010</created_at>
      <id>3958728723</id>
      <text>各位开发者,我们的论坛上线啦~http://sinaurl.cn/h4FWc7 欢迎大家的参与~另外,关于技术相关的问题,可以在论坛上提出,也可以@微博API 这个官方技术支持账号哦~感谢大家对开放平台的支持~[呵呵]</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/>
    </status>
  </recipient>
</direct_message>

JSON Example

{
    "sender_screen_name" : "zhangwei217245",
    "recipient_screen_name" : "微博开放平台",
    "text" : "Wow~~~Congratulations!",
    "recipient" : 
    {
        "name" : "微博开放平台",
        "domain" : "openapi",
        "geo_enabled" : true,
        "followers_count" : 13366,
        "statuses_count" : 158,
        "favourites_count" : 0,
        "city" : "8",
        "description" : "新浪微博开放平台市场推广官方账号,如有技术问题,请@微博API或者发私信给微博API",
        "verified" : true,
        "status" : 
        {
            "created_at" : "Mon Nov 29 16:08:43 +0800 2010",
            "text" : "各位开发者,我们的论坛上线啦~http://sinaurl.cn/h4FWc7 欢迎大家的参与~另外,关于技术相关的问题,可以在论坛上提出,也可以@微博API 这个官方技术支持账号哦~感谢大家对开放平台的支持~[呵呵]",
            "truncated" : false,
            "in_reply_to_status_id" : "",
            "in_reply_to_screen_name" : "",
            "geo" : null,
            "favorited" : false,
            "in_reply_to_user_id" : "",
            "id" : 3958728723,
            "source" : "<a href=\"http://t.sina.com.cn\" rel=\"nofollow\">新浪微博</a>"
        },
        "id" : 11051,
        "gender" : "m",
        "friends_count" : 5,
        "screen_name" : "微博开放平台",
        "allow_all_act_msg" : true,
        "following" : false,
        "url" : "http://open.t.sina.com.cn/",
        "profile_image_url" : "http://tp4.sinaimg.cn/11051/50/1280283165/1",
        "created_at" : "Wed Jan 20 00:00:00 +0800 2010",
        "province" : "11",
        "location" : "北京 海淀区"
    },
    "sender" : 
    {
        "name" : "zhangwei217245",
        "domain" : "zhangwei217245",
        "geo_enabled" : true,
        "followers_count" : 24,
        "statuses_count" : 118,
        "favourites_count" : 3,
        "city" : "5",
        "description" : "ABCDE",
        "verified" : false,
        "status" : 
        {
            "created_at" : "Sat Dec 04 23:35:20 +0800 2010",
            "text" : "动物丛书的由来。。。http://sinaurl.cn/hbVjvr",
            "truncated" : false,
            "in_reply_to_status_id" : "",
            "annotations" : 
            [

            ],
            "in_reply_to_screen_name" : "",
            "geo" : null,
            "favorited" : false,
            "in_reply_to_user_id" : "",
            "id" : 4087921127,
            "source" : "<a href=\"http://t.sina.com.cn\" rel=\"nofollow\">新浪微博</a>"
        },
        "id" : 1658122963,
        "gender" : "m",
        "friends_count" : 44,
        "screen_name" : "zhangwei217245",
        "allow_all_act_msg" : false,
        "following" : false,
        "url" : "",
        "profile_image_url" : "http://tp4.sinaimg.cn/1658122963/50/1282754213",
        "created_at" : "Tue Nov 03 00:00:00 +0800 2009",
        "province" : "11",
        "location" : "北京 朝阳区"
    },
    "sender_id" : 1658122963,
    "recipient_id" : 11051,
    "id" : 722715041,
    "created_at" : "Mon Dec 06 11:21:31 +0800 2010"
}

Field Description - direct_message

  • id: direct message ID
  • text: direct message content
  • sender_id:sender’s UID
  • recipient_id: revipient’s UID
  • created_at: created time
  • sender_screen_name: sender’s nickname
  • recipient_screen_name:recipient’s nickname
  • sender: sender’s profile, see user specification
  • recipient: recipient’s profile, see user specification

Field Description - user

  • id: User ID
  • screen_name: User’s nickname displayed on the home page
  • name: Friendly displayed name, the same as screen_name
  • province: Province code ( see Province and city code table)
  • city: City code ( see Province and city code table)
  • location:Address
  • description: Personal description
  • url: Url of the user’s blog
  • profile_image_url: Profile image
  • domain: The user’s personalized weibo url
  • gender: Gender, m—male, f- - female, n-- unknown
  • followers_count: Followers count
  • friends_count: Following count
  • statuses_count: Weibo count
  • favourites_count: Favorites count
  • created_at: Created time
  • following: Whether the current user is following the user that posts the weibo (Not supported yet)
  • verified: Whether the user is verified by his real identity, marked with “V”

Others

Java Example

Please download Java SDK from . Weibo SDK Development Kit Dowload Site
Sample Code:

package weibo4j.examples;

import weibo4j.DirectMessage;
import weibo4j.Weibo;
import weibo4j.WeiboException;

public class SendDirectMessage {
   /**
    * Usage: java Weibo4j.examples.DirectMessage senderID senderPassword message recipientId
    * @param args String[]
    */
   public static void main(String[] args) {
   	System.setProperty("Weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
   	System.setProperty("Weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
   	
       if (args.length < 4) {
           System.out.println("No WeiboID/Password specified.");
           System.out.println("Usage: java Weibo4j.examples.DirectMessage senderID senderPassword  recipientId message");
           System.exit( -1);
       }
       Weibo weibo = new Weibo(args[0], args[1]);
       try {
          DirectMessage message = weibo.sendDirectMessage(args[2], args[3]);
           System.out.println("Direct message successfully sent to " +
                              message.getRecipientScreenName());
           System.exit(0);
       } catch (WeiboException te) {
           System.out.println("Failed to send message: " + te.getMessage());
           System.exit( -1);
       }
   }
}

PHP Example

Please download PHP SDK with OAUTH supported from Weibo SDK Development Kit Dowload Site
. Sample Code:

//Direct messages/new
$c = new WeiboClient( WB_AKEY , 
                      WB_SKEY , 
                      $_SESSION['last_key']['oauth_token'] , 
                      $_SESSION['last_key']['oauth_token_secret']  );
//user_id
$u_id = "User_ID";
//text
$text = "Text";
$msg = $c->send_dm($u_id,$text);
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;
}
echo("Direct message successfully sent to ".$msg['recipient_screen_name']);



文档更新时间: 2011-01-20