从 PowerApps 连接到 TwitterConnect to Twitter from PowerApps

Twitter

可以通过 Twitter 发布推文,并获取 Twitter 帐户的推文、时间线、好友和关注者。Twitter lets you post tweets and get tweets, timeline, friends, and followers from your Twitter account.

可以在应用上的标签中显示此信息。You can display this information in a label on your app. 例如,可以添加一个输入文本框,要求用户输入一些推文文本,然后添加用于“发布”推文的按钮。For example, you can add an input text box, ask the user to enter in some Tweet text, and then add a button that "posts" the tweet. 可以使用类似的方法来获取推文或搜索推文,然后在应用中的标签或库控件中显示文本。You can use similar methods to get a tweet or search for a tweet, and then display the text in a label or gallery control in your app.

本主题演示如何创建 Twitter 连接,如何在应用中使用 Twitter 连接,并列出可用的函数。This topic shows you how to create the Twitter connection, use the Twitter connection in an app, and lists the available functions.

先决条件Prerequisites

连接到 TwitterConnect to Twitter

  1. 打开 PowerApps,选择“新建”,然后创建一个“空白应用”。Open PowerApps, select New, and then create a Blank app. 选择手机或平板电脑布局。Choose phone or tablet layout. 平板电脑布局为你提供了多个工作区:Tablet layout gives you more workspace:

    打开一个空白应用

  2. 在右侧窗格中,单击或点击“数据源”选项卡,然后单击或点击“添加数据源”。In the right-hand pane, click or tap the Data tab, and then click or tap Add data source.
  3. 选择“新建连接”,然后选择“Twitter”:Select New connection, and then select Twitter:

    连接到 Twitter

    连接到 Twitter

  4. 选择“连接”,输入 Twitter 登录凭据,然后选择“授权应用”。Select Connect, enter your Twitter sign in credentials, and then select Authorize app.
  5. 选择“添加数据源”。Select Add Data Source. 连接显示在“数据源”下:Your connection appears under Data sources:
    关闭选项窗格Close the Options pane

Twitter 连接已创建并已添加到你的应用。The Twitter connection has been created, and added to your app. 现在可供使用。Now, it's ready to be used.

在应用中使用 Twitter 连接Use the Twitter connection in your app

显示时间线Show a timeline

  1. 在“插入”菜单上,选择“库”,然后添加任意带有文本的库。On the Insert menu, select Gallery, and add any of the With text galleries.
  2. 我们来看一些时间线:Let's show some timelines:

    • 若要显示当前用户的时间线,将库的 Items 属性设置为以下公式:To show the current user's timeline, set the Items property of the gallery to the following formulas:

      Twitter.HomeTimeline().TweetText
      Twitter.HomeTimeline({maxResults:3}).TweetText

    • 若要显示其他用户的时间线,将库的 Items 属性设置为以下公式:To show another user's timeline, set the Items property of the gallery to the following formula:

      Twitter.UserTimeline( *TwitterHandle* ).TweetText

      输入用双引号括住的 Twitter 用户名或等效值。Enter a Twitter handle in double quotation marks or an equivalent value. 例如,直接在公式表达式中输入 "satyanadella""powerapps"For example, enter "satyanadella" or "powerapps" directly in the formula expression.

    • 添加一个名为“Tweep”的文本输入控件,并将其 Default 属性设置为 Tweep.TextAdd a text input control named Tweep, and set its Default property to Tweep.Text. 在 Tweep 文本框中,键入 Twitter 用户名,如 satyanadella(不加引号和 @ 符号)。In the Tweep text box, type in a Twitter handle such as satyanadella (without quotation marks and without the @ symbol).

      在库控件中,将 Items 属性设置为以下公式:In the gallery control, set the Items property to the following formula:

      Twitter.UserTimeline(Tweep.Text, {maxResults:5}).TweetText

      库控件将自动显示键入的 Twitter 用户名的推文。The gallery control automatically shows the tweets of the Twitter handler you type in.

      提示

      部分公式使用 maxResults 参数显示时间线中最新推文的 x 数量。Some of these formulas use the maxResults argument to show the x number of most recent tweets in a timeline.

  3. 将库的 Items 属性设置为 Twitter.HomeTimeline()Set the gallery's Items property to Twitter.HomeTimeline().

    选择库后,右侧窗格显示该库的选项。With the gallery selected, the right-hand pane shows options for that gallery.

  4. 在第一个列表中选择“TweetText”,在第二个列表中选择“TweetedBy”,然后在第三个列表中选择“CreatedAt”。Select TweetText in the first list, select TweetedBy in the second list, and select CreatedAt in the third list.

    库现在显示所选属性的值。The gallery now shows the values of the properties you chose.

显示关注者Show followers

  1. 通过使用带有文本的库,可以显示部分关注者:Using a With text gallery, let's show some followers:

    • 若要显示当前用户的关注者,将库的 Items 属性设置为以下公式:To show the current user's followers, set the Items property of the gallery to the following formula:

      Twitter.MyFollowers()
      Twitter.MyFollowers({maxResults:3})

    • 若要显示其他用户的关注者,将库的 Items 属性设置为以下公式:To show the another user's followers, set the Items property of the gallery to the following formula:

      Twitter.Followers( *TwitterHandle* )

      输入用双引号括住的 Twitter 用户名或等效值。Enter a Twitter handle in double quotation marks or an equivalent value. 例如,直接在公式表达式中输入 "satyanadella""powerapps"For example, enter "satyanadella" or "powerapps" directly in the formula expression.

    • 添加一个名为“Tweep”的文本输入控件,并将其 Default 属性设置为 Tweep.TextAdd a text input control named Tweep, and set its Default property to Tweep.Text. 在 Tweep 文本框中,键入 Twitter 用户名,如 satyanadella(不加引号和 @ 符号)。In the Tweep text box, type in a Twitter handle such as satyanadella (without quotation marks and without the @ symbol).

      在库控件中,将 Items 属性设置为以下公式:In the gallery control, set the Items property to the following formula:

      Twitter.Followers(Tweep.Text, {maxResults:5})

      库控件自动显示关注你所键入的 Twitter 用户名的用户。The gallery control automatically shows who is following the Twitter handle you type in.

      提示

      部分公式使用 maxResults 参数显示时间线中最新推文的 x 数量。Some of these formulas use the maxResults argument to show the x number of most recent tweets in a timeline.

  2. 将库的 Items 属性设置为 Twitter.MyFollowers()Set the gallery's Items property to Twitter.MyFollowers().

    选择库后,右侧窗格显示该库的选项。With the gallery selected, the right-hand pane shows options for that gallery.

  3. 在第二个列表中选择“UserName”,在第三个列表中选择“FullName”。Select UserName in the second list, and select FullName in the third list.

    库现在显示所选属性的值。The gallery now shows the values of the properties you chose.

显示关注的用户Show followed users

  1. 通过使用带有文本的库,可以显示一些关注的用户:Using a With text gallery, let's show some followed users:

    • 若要显示当前用户关注了哪些用户,将库的 Items 属性设置为以下公式:To show which users the current user is following, set the Items property of the gallery to the following formula:

      Twitter.MyFollowing()
      Twitter.MyFollowing({maxResults:3})

    • 若要显示其他用户关注了哪些用户,将库的 Items 属性设置为以下公式:To show which users another user is following, set the Items property of the gallery to to the following formula:

      Twitter.Following( *TwitterHandle* )

      输入用双引号括住的 Twitter 用户名或等效值。Enter a Twitter handle in double quotation marks or an equivalent value. 例如,直接在公式表达式中输入 "satyanadella""powerapps"For example, enter "satyanadella" or "powerapps" directly in the formula expression.

    • 添加一个名为“Tweep”的文本输入控件,并将其 Default 属性设置为 Tweep.TextAdd a text input control named Tweep, and set its Default property to Tweep.Text. 在 Tweep 文本框中,键入 Twitter 用户名,如 satyanadella(不加引号和 @ 符号)。In the Tweep text box, type in a Twitter handle such as satyanadella (without quotation marks and without the @ symbol).

      在库控件中,将 Items 属性设置为以下公式:In the gallery control, set the Items property to the following formula:

      Twitter.Following(Tweep.Text, {maxResults:5})

      库控件自动显示你正在关注的其他用户名。The gallery control automatically shows the other handles you are following.

      选择库后,右侧窗格显示该库的选项。With the gallery selected, the right-hand pane shows options for that gallery.

  2. 在“Body1”列表中选择“Description”,在“Heading1” 列表中选择“UserName”,在“Subtitle1” 列表中选择“FullName”。Select Description in the Body1 list, UserName in the Heading1 list, and FullName in the Subtitle1 list.

    库现在显示所选属性的值。The gallery now shows the values of the properties you chose.

显示有关用户的信息Show information about a user

添加一个标签,然后将“Text”属性设置为以下公式之一:Add a label, and then set its Text property to one of these formulas:

  • twitter.User( *TwitterHandle* ).Description
  • twitter.User( *TwitterHandle* ).FullName
  • twitter.User( *TwitterHandle* ).Location
  • twitter.User( *TwitterHandle* ).UserName
  • twitter.User( *TwitterHandle* ).FollowersCount
  • twitter.User( *TwitterHandle* ).FriendsCount
  • twitter.User( *TwitterHandle* ).Id
  • twitter.User( *TwitterHandle* ).StatusesCount

输入用双引号括住的 Twitter 用户名或等效值。Enter a Twitter handle in double quotation marks or an equivalent value. 例如,直接在公式表达式中输入 "satyanadella""powerapps"For example, enter "satyanadella" or "powerapps" directly in the formula expression.

或者,可以使用输入文本控件键入 Twitter 用户名,如本主题中所述。Or, you can use an input text control to type in a Twitter handle, just as we have throughout this topic.

搜索推文Search tweets

  1. 添加带有文本的库,将其 Items 属性设置为下列公式:Using a With text gallery, set its Items property to the following formula:

    Twitter.SearchTweet( *SearchTerm* ).TweetText

    输入用双引号括住的 SearchTerm 或引用等效值。Enter a SearchTerm in double quotation marks or by referring to an equivalent value. 例如,直接在公式中输入 "PowerApps""microsoft"For example, enter "PowerApps" or "microsoft" directly in the formula.

    或者,可以使用输入文本控件指定搜索项,如本主题中所述。Or, you can use an Input text control to specify a search term, just as we have throughout this topic.

    提示

    使用 maxResults 可显示前五个结果:Show the first five results by using maxResults:

    Twitter.SearchTweet(SearchTerm.Text, {maxResults:5}).TweetText

  2. 将库的 Items 属性设置为 Twitter.SearchTweet(SearchTerm.Text, {maxResults:5})Set the gallery's Items property to Twitter.SearchTweet(SearchTerm.Text, {maxResults:5}).

    选择库后,右侧窗格显示该库的选项。With the gallery selected, the right-hand pane shows options for that gallery.

  3. 在第一个列表中选择“TweetText”,第二个列表中选择“TweetedBy”,然后在第三个列表中选择“CreatedAt”。Select TweetText in the first list, TweetedBy in the second list, and CreatedAt in the third list.

    库现在显示所选属性的值。The gallery now shows the values of the properties you chose.

发送推文Send a tweet

  1. 添加一个文本输入控件,然后将其重命名为“MyTweet”。Add a text input control, and then rename it MyTweet.
  2. 添加按钮,然后将其 OnSelect 属性设置为以下公式:Add a button, and then set its OnSelect property to the following formula:
    Twitter.Tweet({tweetText: MyTweet.Text})
  3. 按 F5,或选择“预览”按钮 ()。Press F5, or select the Preview button (). 在“MyTweet”中输入一些文本,然后选择按钮来推送你输入的文本。Type some text into MyTweet, and then select the button to tweet the text that you entered.
  4. 按 Esc 返回默认工作区。Press Esc to return to the default workspace.

查看可用函数View the available functions

此连接包括以下函数:This connection includes the following functions:

函数名称Function Name 说明Description
UserTimelineUserTimeline 检索指定用户发布的最新推文的集合Retrieves a collection of the most recent tweets posted by the specified user
HomeTimelineHomeTimeline 检索我和我的关注者发布的最新推文和转发的推文Retrieves the most recent tweets and re-tweets posted me and my followers
SearchTweetSearchTweet 检索与指定查询匹配的相关推文的集合Retrieves a collection of relevant tweets matching a specified query
FollowersFollowers 检索关注指定用户的用户Retrieves users following the specified user
MyFollowersMyFollowers 检索关注我的用户Retrieves users who are following me
FollowingFollowing 检索指定用户正在关注的用户Retrieves users who the specified user is following
MyFollowingMyFollowing 检索我正在关注的用户Retrieves users that I am following
UserUser 检索指定用户的详细信息(例如:用户名、说明、关注者数量等。)Retrieves details about the specified user (example: user name, description, followers count, etc.)
TweetTweet 推文Tweet
OnNewTweetOnNewTweet 发布与搜索查询匹配的新推文时触发工作流Triggers a workflow when a new tweet is posted which matches your search query

UserTimelineUserTimeline

获取用户时间线:检索指定用户发布的最新推文的集合Get user timeline: Retrieves a collection of the most recent tweets posted by the specified user

输入属性Input properties

名称Name 数据类型Data Type 需要Required 说明Description
userNameuserName 字符串string yes Twitter 用户名Twitter handle
maxResultsmaxResults 整数integer no 要检索的推文的最大数量,例如 {maxResults:5}Maximum number of tweets to retrieve, e.g. {maxResults:5}

输出属性Output properties

属性名称Property Name 数据类型Data Type 需要Required 说明Description
TweetTextTweetText 字符串string Yes
TweetIdTweetId 字符串string No
CreatedAtCreatedAt 字符串string No
RetweetCountRetweetCount 整数integer Yes
TweetedByTweetedBy 字符串string Yes
MediaUrlsMediaUrls 数组array No

HomeTimelineHomeTimeline

获取主页时间线:检索我和我的关注者发布的最新推文和转发的推文Get home timeline: Retrieves the most recent tweets and re-tweets posted me and my followers

输入属性Input properties

名称Name 数据类型Data Type 需要Required 说明Description
maxResultsmaxResults 整数integer no 要检索的推文的最大数量,例如 {maxResults:5}Maximum number of tweets to retrieve, e.g. {maxResults:5}

输出属性Output properties

属性名称Property Name 数据类型Data Type 需要Required 说明Description
TweetTextTweetText 字符串string Yes
TweetIdTweetId 字符串string No
CreatedAtCreatedAt 字符串string No
RetweetCountRetweetCount 整数integer Yes
TweetedByTweetedBy 字符串string Yes
MediaUrlsMediaUrls 数组array No

SearchTweetSearchTweet

搜索推文:检索与指定查询匹配的相关推文的集合Search tweet: Retrieves a collection of relevant tweets matching a specified query

输入属性Input properties

名称Name 数据类型Data Type 需要Required 说明Description
searchQuerysearchQuery 字符串string yes 查询文本(可以使用 Twitter 支持的任何查询运算符:http://www.twitter.com/search)Query text (you may use any Twitter supported query operators: http://www.twitter.com/search)
maxResultsmaxResults 整数integer no 要检索的推文的最大数量,例如 {maxResults:5}Maximum number of tweets to retrieve, e.g. {maxResults:5}

输出属性Output properties

属性名称Property Name 数据类型Data Type 需要Required 说明Description
TweetTextTweetText 字符串string Yes
TweetIdTweetId 字符串string No
CreatedAtCreatedAt 字符串string No
RetweetCountRetweetCount 整数integer Yes
TweetedByTweetedBy 字符串string Yes
MediaUrlsMediaUrls 数组array No

关注者Followers

获取关注者:检索关注指定用户的用户Get followers: Retrieves users following the specified user

输入属性Input properties

名称Name 数据类型Data Type 需要Required 说明Description
userNameuserName 字符串string yes 用户的 Twitter 用户名Twitter handle of the user
maxResultsmaxResults 整数integer no 要检索的用户的最大数量,例如 {maxResults:5}Maximum number of users to retrieve, e.g. {maxResults:5}

输出属性Output properties

属性名称Property Name 数据类型Data Type 需要Required 说明Description
全名FullName 字符串string Yes
位置Location 字符串string Yes
IDId 整数integer No
UserNameUserName 字符串string Yes
FollowersCountFollowersCount 整数integer No
说明Description 字符串string Yes
StatusesCountStatusesCount 整数integer No
FriendsCountFriendsCount 整数integer No

MyFollowersMyFollowers

获取我的关注者:检索关注我的用户Get my followers: Retrieves users who are following me

输入属性Input properties

名称Name 数据类型Data Type 需要Required 说明Description
maxResultsmaxResults 整数integer no 要检索的用户的最大数量,例如 {maxResults:5}Maximum number of users to retrieve, e.g. {maxResults:5}

输出属性Output properties

属性名称Property Name 数据类型Data Type 需要Required 说明Description
全名FullName 字符串string Yes
位置Location 字符串string Yes
IDId 整数integer No
UserNameUserName 字符串string Yes
FollowersCountFollowersCount 整数integer No
说明Description 字符串string Yes
StatusesCountStatusesCount 整数integer No
FriendsCountFriendsCount 整数integer No

关注Following

获取关注的人:检索指定用户正在关注的用户Get following: Retrieves users who the specified user is following

输入属性Input properties

名称Name 数据类型Data Type 需要Required 说明Description
userNameuserName 字符串string yes 用户的 Twitter 用户名Twitter handle of the user
maxResultsmaxResults 整数integer no 要检索的用户的最大数量,例如 {maxResults:5}Maximum number of users to retrieve, e.g. {maxResults:5}

输出属性Output properties

属性名称Property Name 数据类型Data Type 需要Required 说明Description
全名FullName 字符串string Yes
位置Location 字符串string Yes
IDId 整数integer No
UserNameUserName 字符串string Yes
FollowersCountFollowersCount 整数integer No
说明Description 字符串string Yes
StatusesCountStatusesCount 整数integer No
FriendsCountFriendsCount 整数integer No

MyFollowingMyFollowing

获取我关注的人:检索我正在关注的用户Get my following: Retrieves users that I am following

输入属性Input properties

名称Name 数据类型Data Type 需要Required 说明Description
maxResultsmaxResults 整数integer no 要检索的用户的最大数量,例如 {maxResults:5}Maximum number of users to retrieve, e.g. {maxResults:5}

输出属性Output properties

属性名称Property Name 数据类型Data Type 需要Required 说明Description
全名FullName 字符串string Yes
位置Location 字符串string Yes
IDId 整数integer No
UserNameUserName 字符串string Yes
FollowersCountFollowersCount 整数integer No
说明Description 字符串string Yes
StatusesCountStatusesCount 整数integer No
FriendsCountFriendsCount 整数integer No

用户User

获取用户:检索指定用户的详细信息(例如:用户名、说明、关注者数量等。)Get user: Retrieves details about the specified user (example: user name, description, followers count, etc.)

输入属性Input properties

名称Name 数据类型Data Type 需要Required 说明Description
userNameuserName 字符串string yes 用户的 Twitter 用户名Twitter handle of the user

输出属性Output properties

属性名称Property Name 数据类型Data Type 需要Required 说明Description
全名FullName 字符串string Yes
位置Location 字符串string Yes
IDId 整数integer No
UserNameUserName 字符串string Yes
FollowersCountFollowersCount 整数integer No
说明Description 字符串string Yes
StatusesCountStatusesCount 整数integer No
FriendsCountFriendsCount 整数integer No

推文Tweet

发布新推文:推文Post a new tweet: Tweet

输入属性Input properties

名称Name 数据类型Data Type 需要Required 说明Description
TweetTexttweetText 字符串string no 要发布的文本,例如 {tweetText:"hello"}Text to be posted e.g. {tweetText:"hello"}
bodybody 字符串string no 要发布的媒体Media to be posted

输出属性Output properties

属性名称Property Name 数据类型Data Type 需要Required 说明Description
TweetIdTweetId 字符串string Yes

OnNewTweetOnNewTweet

显示新推文时:发布与搜索查询匹配的新推文时触发工作流When a new tweet appears: Triggers a workflow when a new tweet is posted which matches your search query

输入属性Input properties

名称Name 数据类型Data Type 需要Required 说明Description
searchQuerysearchQuery 字符串string yes 查询文本(可以使用 Twitter 支持的任何查询运算符:http://www.twitter.com/search)Query text (you may use any Twitter supported query operators: http://www.twitter.com/search)

输出属性Output properties

属性名称Property Name 数据类型Data Type 需要Required 说明Description
value 数组array No

查看所有可用连接See all the available connections.
了解如何向你的应用添加连接Learn how to add connections to your apps.