<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Twitter on kenji.blog</title><link>http://kenji.blog/zh-tw/tags/twitter/</link><description>Recent content in Twitter on kenji.blog</description><generator>Hugo -- gohugo.io</generator><language>zh-tw</language><copyright>kenjinote</copyright><lastBuildDate>Sat, 08 Apr 2023 18:48:32 +0900</lastBuildDate><atom:link href="http://kenji.blog/zh-tw/tags/twitter/index.xml" rel="self" type="application/rss+xml"/><item><title>如何使用 Twitter API 和 Google Colaboratory 發布推文</title><link>http://kenji.blog/zh-tw/p/how-to-tweet-using-twitter-api-and-google-colaboratory-zh-tw/</link><pubDate>Sat, 08 Apr 2023 18:48:32 +0900</pubDate><guid>http://kenji.blog/zh-tw/p/how-to-tweet-using-twitter-api-and-google-colaboratory-zh-tw/</guid><description>&lt;img src="http://kenji.blog/p/twitter-api%E3%81%A8google-colaboratory%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%A6tweet%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/img.png" alt="Featured image of post 如何使用 Twitter API 和 Google Colaboratory 發布推文" />&lt;h1 id="所需項目">所需項目
&lt;/h1>&lt;ul>
&lt;li>Twitter API&lt;/li>
&lt;li>Twitter API SECRET&lt;/li>
&lt;li>Twitter ACCESS TOKEN&lt;/li>
&lt;li>Twitter ACCESS TOKEN SECRET&lt;/li>
&lt;li>Google 帳號&lt;/li>
&lt;/ul>
&lt;p>有關如何取得 Twitter API，請參閱參考網站。&lt;/p>
&lt;h1 id="使用-api-發布推文的步驟">使用 API 發布推文的步驟
&lt;/h1>&lt;ol>
&lt;li>進入 &lt;a class="link" href="https://colab.research.google.com/" target="_blank" rel="noopener"
>https://colab.research.google.com/&lt;/a>&lt;/li>
&lt;li>選擇「檔案」→「新增筆記本」&lt;/li>
&lt;li>貼上並執行以下程式碼（請使用您自己取得的實際值）&lt;/li>
&lt;/ol>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">API_KEY = &amp;#39;9Smu2f2RoLqbVQHQq6n79Z2JW&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">API_SECRET = &amp;#39;uGVRIkLL2l8sRyPv2Lr4mXxXppnQF1isMoRnvktcXCtFgAK2R8&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">ACCESS_TOKEN = &amp;#39;0367292979164670705-7hSErDoQbO6fkFtnn5UY0vqpvecy0O&amp;#39;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">ACCESS_TOKEN_SECRET = &amp;#39;pUv81U9GVzZirz5g4AxZPHAJ4GpSXnBo8GUcZ1egtjw9q&amp;#39;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;ol start="3">
&lt;li>貼上並執行以下程式碼&lt;/li>
&lt;/ol>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">import tweepy
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;ol start="4">
&lt;li>貼上並執行以下程式碼（API v1.1）&lt;/li>
&lt;/ol>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">auth = tweepy.OAuthHandler(API_KEY, API_SECRET)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">api = tweepy.API(auth)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">api.update_status(&amp;#34;hello&amp;#34;)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>→ 將發布一則內容為 &lt;code>hello&lt;/code> 的推文&lt;/p>
&lt;ol start="5">
&lt;li>貼上並執行以下程式碼（API v2.0）&lt;/li>
&lt;/ol>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">client = tweepy.Client(consumer_key=API_KEY, consumer_secret=API_SECRET, access_token=ACCESS_TOKEN, access_token_secret=ACCESS_TOKEN_SECRET)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">client.create_tweet(text=&amp;#39;hello v2&amp;#39;)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>→ 將發布一則內容為 &lt;code>hello v2&lt;/code> 的推文&lt;/p>
&lt;p>以上&lt;/p>
&lt;h1 id="參考">參考
&lt;/h1>&lt;ul>
&lt;li>&lt;a class="link" href="https://bloomtectec.com/twitter-api-application-procedure/" target="_blank" rel="noopener"
>【截至2021年4月】附帶使用範例與截圖的 Twitter API 使用申請徹底解說&lt;/a>&lt;/li>
&lt;li>&lt;a class="link" href="https://bloomtectec.com/use-twitter-api-in-google-colab/" target="_blank" rel="noopener"
>【無需繁瑣設定！】Google Colaboratory 是 Twitter API 測試環境的推薦選擇【附帶原始碼分享】&lt;/a>&lt;/li>
&lt;li>&lt;a class="link" href="https://3pysci.com/tweepy-28/" target="_blank" rel="noopener"
>【Tweepy】Twitter API v2：推文、回覆、附帶投票的推文、附帶媒體的推文（v1.1）[Python]&lt;/a>&lt;/li>
&lt;/ul></description></item><item><title>如何在 PaperMod 中設定 Twitter Card</title><link>http://kenji.blog/zh-tw/p/papermod%E3%81%A7twitter-card%E3%82%92%E8%A8%AD%E5%AE%9A%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/</link><pubDate>Sat, 10 Sep 2022 18:41:22 +0900</pubDate><guid>http://kenji.blog/zh-tw/p/papermod%E3%81%A7twitter-card%E3%82%92%E8%A8%AD%E5%AE%9A%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/</guid><description>&lt;img src="http://kenji.blog/p/papermod%E3%81%A7twitter-card%E3%82%92%E8%A8%AD%E5%AE%9A%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95/images/img.png" alt="Featured image of post 如何在 PaperMod 中設定 Twitter Card" />&lt;h1 id="簡介">簡介
&lt;/h1>&lt;p>PaperMod 主題支援 Twitter Card。
但是，Twitter Card 的設定必須寫在 &lt;code>config.toml&lt;/code> 或每篇文章的 &lt;code>*.md&lt;/code> 的標頭資訊中。
如果您在每篇文章和 &lt;code>config.toml&lt;/code> 中都進行了設定，則每篇文章的標頭資訊將優先。&lt;/p>
&lt;h1 id="設定方法">設定方法
&lt;/h1>&lt;h2 id="configtoml">config.toml
&lt;/h2>&lt;p>在 &lt;code>config.toml&lt;/code> 中，於 &lt;code>[params]&lt;/code> 下方新增一個名為 &lt;code>images&lt;/code> 的項目。
在 &lt;code>images&lt;/code> 中，描述要在 Twitter Card 顯示的圖片路徑。
如果您將圖片放在 &lt;code>static&lt;/code> 資料夾中，只需指定檔案名稱即可。&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">[params]
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> images = [&amp;#34;twitter_card.jpg&amp;#34;]
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;p>資料夾結構&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt"> 1
&lt;/span>&lt;span class="lnt"> 2
&lt;/span>&lt;span class="lnt"> 3
&lt;/span>&lt;span class="lnt"> 4
&lt;/span>&lt;span class="lnt"> 5
&lt;/span>&lt;span class="lnt"> 6
&lt;/span>&lt;span class="lnt"> 7
&lt;/span>&lt;span class="lnt"> 8
&lt;/span>&lt;span class="lnt"> 9
&lt;/span>&lt;span class="lnt">10
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">root
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">│ config.toml (寫在這裡)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">├─content
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">│ └─posts
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">│ └─文章資料夾
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">│ │ index.md (寫在這裡)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">│ └─images
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">│ cover.png (放在這裡)
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">└─static
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> twitter_card.jpg (放在這裡)
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h2 id="每篇文章的標頭資訊">每篇文章的標頭資訊
&lt;/h2>&lt;p>在每篇文章的標頭資訊中，於 &lt;code>cover&lt;/code> 下方新增一個名為 &lt;code>image&lt;/code> 的項目。
如果將 &lt;code>relative&lt;/code> 設定為 &lt;code>true&lt;/code>，您可以透過文章的 &lt;code>*.md&lt;/code> 的相對路徑來指定。&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">cover:
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> image: &amp;#34;images/cover.jpg&amp;#34;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> relative: true
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h3 id="如果不想顯示在文章頂部">如果不想顯示在文章頂部
&lt;/h3>&lt;p>如果您不想在文章頂部顯示封面圖片，請在 &lt;code>cover&lt;/code> 下方新增一個名為 &lt;code>hidden&lt;/code> 的項目，並將其設定為 &lt;code>true&lt;/code>。&lt;/p>
&lt;div class="highlight">&lt;div class="chroma">
&lt;table class="lntable">&lt;tr>&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code>&lt;span class="lnt">1
&lt;/span>&lt;span class="lnt">2
&lt;/span>&lt;span class="lnt">3
&lt;/span>&lt;span class="lnt">4
&lt;/span>&lt;/code>&lt;/pre>&lt;/td>
&lt;td class="lntd">
&lt;pre tabindex="0" class="chroma">&lt;code class="language-fallback" data-lang="fallback">&lt;span class="line">&lt;span class="cl">cover:
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> image: &amp;#34;images/cover.jpg&amp;#34;
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> relative: true
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> hidden: true
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/td>&lt;/tr>&lt;/table>
&lt;/div>
&lt;/div>&lt;h1 id="關於圖片大小">關於圖片大小
&lt;/h1>&lt;p>在 PaperMod 目前的規格中，Twitter Card 的大小似乎只支援 &lt;code>summary_large_image&lt;/code>。
對於 &lt;code>summary_large_image&lt;/code> 的合適大小（解析度）有各種說法，但大約 &lt;code>800 x 418&lt;/code>（圖片比例 1.91:1）似乎不錯。&lt;/p>
&lt;p>&lt;a class="link" href="https://developer.twitter.com/en/docs/twitter-for-websites/cards/overview/summary-card-with-large-image" target="_blank" rel="noopener"
>參考網站 1&lt;/a>
&lt;a class="link" href="https://developers.facebook.com/docs/sharing/best-practices" target="_blank" rel="noopener"
>參考網站 2&lt;/a>&lt;/p>
&lt;p>如果可能的話，建議在發布前調整圖片大小。&lt;/p>
&lt;h1 id="如何檢查設定">如何檢查設定
&lt;/h1>&lt;p>要檢查 Twitter Card 的設定，請使用 &lt;a class="link" href="https://cards-dev.twitter.com/validator" target="_blank" rel="noopener"
>Twitter Card Validator&lt;/a>。
但是，由於預覽在我的環境中無法正確顯示，如果無法顯示預覽，我建議您在發布前使用私人帳號等檢查一次。&lt;/p></description></item></channel></rss>