推特開發者賬號申請失敗【推特開發者文檔系列7】——通過API接口發佈、檢索推文

本系列是對推特開發者文檔進行的翻譯,以便幫助開發人員使用API接口,難免有些地方存在不足,還請諒解。

關於如何獲得一個推特開發者賬號請看此貼:

推特開發者賬號的申請

搜索微信公衆號 twitterDeveloper 獲得幫助


Post, retrieve, and engage with Tweets

通過以下API接口可以通過編程的方式創建、檢索和刪除Tweets、Retweets和Like:

Tweets(發推文) Retweets(轉推) Likes(點贊)
POST statuses/update
POST statuses/destroy/:id
GET statuses/show/:id
GET statuses/oembed
GET statuses/lookup
POST statuses/retweet/:id
POST statuses/unretweet/:id
GET statuses/retweets/:id
GET statuses/retweets_of_me
GET statuses/retweeters/ids
POST favorites/create/:id
POST favorites/destroy/:id
GET favorites/list

有關更多詳細信息,請參閱 API文檔部分中的各個端口信息 。


示例

通過API發推文

對於每次更新嘗試,都會將更新文本與驗證用戶的最新推文進行比較。 任何可能導致重複的嘗試都將被阻止,從而導致403錯誤。 用戶不能連續兩次提交相同的狀態。(一次POST有且僅生效一次)

儘管不受API的速率限制,但用戶一次只能創建的Tweets數量受到限制。 如果用戶發佈的更新數量達到當前允許的限制,則此方法將返回HTTP 403錯誤。

關於地理位置

  • 如果爲用戶提供的geo_enabled爲false(此設置是所有用戶的默認設置,除非用戶已在其設置中啓用地理定位),更新中的所有地理標記參數都將被忽略。
  • lat跟蹤傳遞給小數點分隔符後的位數(最多8位),以便當lat在狀態對象中返回時,小數點分隔符後將具有相同的位數。
  • 使用小數點作爲緯度和經度的分隔符(而不是小數逗號)
  • 對於JSON,響應主要使用GeoJSON中描述的約定。 但是,Twitter渲染的geo對象座標與GeoJSON規範相反。 GeoJSON指定了一個經度然後是一個緯度,而Twitter則將其表示爲一個緯度然後是經度:“ geo”:{“ type”:“ Point”,“ coordinates”:[37.78217,-122.40062]}
  • coordinates對象將替換geo對象(尚未設置geo對象的棄用日期)-區別在於,coordinates對象(以JSON)現在可以在GeoJSON中正確呈現。
  • 如果將place_id傳遞到狀態更新中,則該位置將附加到狀態中。 如果未明確提供place_id,但提供了緯度和經度,則API會嘗試通過調用geo / reverse_geocode隱式提供位置。
  • 用戶可以通過用戶設置頁面從其所有推文中刪除所有地理標籤。 目前,尚無從單個Tweet移除地理標籤的方法。

接口url

https://api.twitter.com/1.1/statuses/update.json

資源信息

響應格式 Json
是否需要認證 是(僅限用戶上下文)
頻率限制
請求/3小時窗口期 每位用戶300 ;每個應用300

注:每3小時300次的限制是POST statuses/retweet/:id :端點數目的總和。 您在3個小時內只能發佈300條Tweets或Retweets。

參數

名稱 是否必要 描述 默認值 示例
status 需要 狀態更新的文本。 URL根據需要進行編碼。 t.co鏈接換行將影響字符計數。
in_reply_to_status_id 可選 更新正在答覆的現有狀態的ID。注意:此參數將被忽略,除非狀態文本中提到此參數引用的Tweet的作者。因此,您必須在更新中包含@username,其中username是引用Tweet的作者。
auto_populate_reply_metadata 可選 如果設置爲true並與in_reply_to_status_id一起使用,則將從原始Tweet中查找前導@提到,並從此處添加到新Tweet中。隨着回覆鏈的增長,這將在擴展Tweet的元數據中追加@insertions,直到達到@insertions的限制。如果原始推文被刪除,回覆將失敗。 false true
exclude_reply_user_ids 可選 當與auto_populate_reply_元數據一起使用時,一個逗號分隔的用戶id列表將從服務器上刪除,該列表將在擴展的Tweet上顯示@references前綴。請注意,不能刪除前導的@notice,因爲它會破壞in reply to status id語義。嘗試刪除它將被忽略。 786491,54931584
lat 可選 此Tweet所指位置的緯度。此參數將被忽略,除非它在-90.0到+90.0(北爲正)範圍內。如果沒有相應的long參數,也將忽略它。 37.7821120598956
long 可選 此Tweet所指位置的經度。經度的有效範圍是-180.0到+180.0(東爲正)。如果超出該範圍,如果不是數字,如果啓用了地理座標,或者沒有相應的lat參數,則將忽略此參數。 -122.400612831116
place_id 可選 世界上的一個地方 df51dec6f4ee2b2c
display_coordinates 可選 是否在發送推文的確切座標上放置圖釘。 true
trim_user 可選 當設置爲true、t或1時,響應將包含僅包含作者ID的用戶對象。省略此參數可接收完整的用戶對象。 false true
enable_dmcommands 可選 設置爲true時,啓用快捷方式命令,將直接消息作爲狀態文本的一部分發送給用戶。如果設置爲false,則禁用此行爲,並在發佈的狀態文本中包含任何前導字符 false true
fail_dmcommands 可選 設置爲true時,使以shortcode命令開頭的任何狀態文本都返回API錯誤。 設置爲false時,允許在狀態文本中發送簡碼命令並由API進行操作。 true false
card_uri 可選 使用來自任何廣告卡響應的card-uri值將廣告卡與Tweet關聯。 card://853503245793641682
attachment_url 可選 爲了使URL不計入擴展Tweet的狀態正文中,請提供一個URL作爲Tweet附件。此URL必須是Tweet永久鏈接或直接消息深度鏈接。任意的非Twitter url必須保留在狀態文本中。傳遞給attachment_url參數的url與Tweet permalink或Direct Message deep link不匹配,將在創建Tweet時失敗並導致異常。 https://twitter.com/andypiper/status/903615884664725505
media_ids 可選 與Tweet關聯的media_id的逗號分隔列表。 您最多可以在Tweet中包含4張照片或1個GIF動畫或1個視頻。 有關上傳媒體的更多詳細信息,請參見上傳媒體。 471592142565957632
possibly_sensitive 可選 如果上載的Tweet媒體可能被視爲敏感內容(如裸體或醫療程序),則必須將此值設置爲true。有關更多內容,請參見媒體設置和最佳實踐。 false true

請求示例

要獲取生成的oauth_nonce,oauth_token和oauth_signature,可以使用REST工具,例如Insomnia或Postman。

curl -XPOST 
  --url 'https://api.twitter.com/1.1/statuses/update.json?status=hello' 
  --header 'authorization: OAuth
  oauth_consumer_key="oauth_customer_key",
  oauth_nonce="generated_oauth_nonce",
  oauth_signature="generated_oauth_signature",
  oauth_signature_method="HMAC-SHA1",
  oauth_timestamp="generated_timestamp",
  oauth_token="oauth_token",
  oauth_version="1.0"'

很多人想把狀態從“hello”改成其他的。

您還可以使用任何其他想要的OAuth幫助程序庫,比如twurl。
$ twurl -d 'status=Test tweet using the POST statuses/update endpoint' /1.1/statuses/update.json

響應例子

請求成功後,將返回以下內容:

{
  "created_at": "Wed Oct 10 20:19:24 +0000 2018",
  "id": 1050118621198921700,
  "id_str": "1050118621198921728",
  "text": "To make room for more expression, we will now count all emojis as equal—including those with gender‍‍‍ ‍‍and skin t… https://t.co/MkGjXf9aXm",
  "source": "<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>",
  "truncated": true,
  "in_reply_to_status_id": null,
  "in_reply_to_status_id_str": null,
  "in_reply_to_user_id": null,
  "in_reply_to_user_id_str": null,
  "in_reply_to_screen_name": null,
  "user": {
    "id": 6253282,
    "id_str": "6253282",
    "name": "Twitter API",
    "screen_name": "TwitterAPI",
    "location": "San Francisco, CA",
    "url": "https://developer.twitter.com",
    "description": "The Real Twitter API. Tweets about API changes, service issues and our Developer Platform. Don't get an answer? It's on my website.",
    "translator_type": "null",
    "derived": {
      "locations": [
        {
          "country": "United States",
          "country_code": "US",
          "locality": "San Francisco",
          "region": "California",
          "sub_region": "San Francisco County",
          "full_name": "San Francisco, California, United States",
          "geo": {
            "coordinates": [
              -122.41942,
              37.77493
            ],
            "type": "point"
          }
        }
      ]
    },
    "protected": false,
    "verified": true,
    "followers_count": 6172196,
    "friends_count": 12,
    "listed_count": 13003,
    "favourites_count": 31,
    "statuses_count": 3650,
    "created_at": "Wed May 23 06:01:13 +0000 2007",
    "utc_offset": null,
    "time_zone": null,
    "geo_enabled": false,
    "lang": "en",
    "contributors_enabled": false,
    "is_translator": null,
    "profile_background_color": "null",
    "profile_background_image_url": "null",
    "profile_background_image_url_https": "null",
    "profile_background_tile": null,
    "profile_link_color": "null",
    "profile_sidebar_border_color": "null",
    "profile_sidebar_fill_color": "null",
    "profile_text_color": "null",
    "profile_use_background_image": null,
    "profile_image_url": "null",
    "profile_image_url_https": "https://pbs.twimg.com/profile_images/942858479592554497/BbazLO9L_normal.jpg",
    "profile_banner_url": "https://pbs.twimg.com/profile_banners/6253282/1497491515",
    "default_profile": false,
    "default_profile_image": false,
    "following": null,
    "follow_request_sent": null,
    "notifications": null
  },
  "geo": null,
  "coordinates": null,
  "place": null,
  "contributors": null,
  "is_quote_status": false,
  "extended_tweet": {
    "full_text": "To make room for more expression, we will now count all emojis as equal—including those with gender‍‍‍ ‍‍and skin tone modifiers 👍🏻👍🏽👍🏿. This is now reflected in Twitter-Text, our Open Source library. nnUsing Twitter-Text? See the forum post for detail: https://t.co/Nx1XZmRCXA",
    "display_text_range": [
      0,
      277
    ],
    "entities": {
      "hashtags": [],
      "urls": [
        {
          "url": "https://t.co/Nx1XZmRCXA",
          "expanded_url": "https://twittercommunity.com/t/new-update-to-the-twitter-text-library-emoji-character-count/114607",
          "display_url": "twittercommunity.com/t/new-update-t…",
          "unwound": {
            "url": "https://twittercommunity.com/t/new-update-to-the-twitter-text-library-emoji-character-count/114607",
            "status": 200,
            "title": "New update to the Twitter-Text library: Emoji character count",
            "description": "Over the years, we have made several updates to the way that people can communicate on Twitter. One of the more notable changes made last year was to increase the number of characters per Tweet from 140 to 280 characters. Today, we continue to expand people’s ability to express themselves by announcing a change to the way that we count emojis. Due to the differences in the way written text and emojis are encoded, many emojis (including emojis where you can apply gender and skin tone) have count..."
          },
          "indices": [
            254,
            277
          ]
        }
      ],
      "user_mentions": [],
      "symbols": []
    }
  },
  "quote_count": 0,
  "reply_count": 0,
  "retweet_count": 0,
  "favorite_count": 0,
  "entities": {
    "hashtags": [],
    "urls": [
      {
        "url": "https://t.co/MkGjXf9aXm",
        "expanded_url": "https://twitter.com/i/web/status/1050118621198921728",
        "display_url": "twitter.com/i/web/status/1…",
        "indices": [
          117,
          140
        ]
      }
    ],
    "user_mentions": [],
    "symbols": []
  },
  "favorited": false,
  "retweeted": false,
  "possibly_sensitive": false,
  "filter_level": "low",
  "lang": "en"
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章