<?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-cn/tags/twitter/</link><description>Recent content in Twitter on kenji.blog</description><generator>Hugo -- gohugo.io</generator><language>zh-cn</language><copyright>kenjinote</copyright><lastBuildDate>Sat, 08 Apr 2023 18:48:32 +0900</lastBuildDate><atom:link href="http://kenji.blog/zh-cn/tags/twitter/index.xml" rel="self" type="application/rss+xml"/><item><title>使用 Twitter API 和 Google Colaboratory 发送推文的方法</title><link>http://kenji.blog/zh-cn/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/</link><pubDate>Sat, 08 Apr 2023 18:48:32 +0900</pubDate><guid>http://kenji.blog/zh-cn/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/</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"
>【免去繁琐设置！】想要体验Twitter API，强烈推荐Google Colaboratory【内附源码】&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-cn/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-cn/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>