Get Data from Twitter API with Power Query

37 Comments

  1. Thanks for the post! Tried it out on PowerBI Designer and it was great to see the tweets coming in. The one thing that is confusing me is that when using the #SQLCruise hashtag it only downloaded 12 tweets. When I use TweetDeck I get way more than 12. Any ideas on where to look

    1. Author

      Hey Howard, I’m glad to hear that you got it working! I figured out what the discrepancy is between this Power Query example and TweetDeck. TweetDeck is using the GET users/search method instead of the GET search/tweets method, hence the different results.

      The bad news is that the GET users/tweets method requires a different authentication mechanism with the Twitter API that involves user context (And greater difficulty). My example above uses “Application-only authentication”.

      Hopefully I can find time to try and get that the other authentication mechanism working in Power Query, as it opens up even more options. In theory I believe you could post Tweets from Excel/Power Query 🙂

      Also – great name btw!

  2. Hi Chris, this was super helpful in accessing the search API, but do you know why it might not authenticate when using “https://api.twitter.com/1.1/statuses/user_timeline.json?” or similar GETs? I’m looking to set up queries to pull from these and it looks like the only other method is to change my authentification timestamp, nonce, etc. each time I need to refresh. Thanks for this, this has been very helpful!

    Bo

    1. Author

      Hey Bo, I’m glad that you found the post useful. The user timeline method appears to be available using the Application-only authentication method described in this post, but I have not tried that one personally. What is the exact error that you’re seeing?

      1. Thanks for the reply, Chris! I’m getting “The user was not authorized”, and it won’t authenticate on anonymous or anything.

  3. Thanks for the help Chris – got it working yesterday. And then today I saw the info on the new Office Graph APIs, and wondered if a similar mechanism would work with that too. My skill level was too low to find out, but wondered if you’re looking at it with the same question?
    https://graph.microsoft.io/

    (Scenarios I’m thinking about are things like the “Get People I work with” feed which would be interesting for scenarios with students and researchers in universities)

    1. Author

      Hi Ray, I would imagine that the process would be very similar. I have not done any work with the Microsoft Graph, however, so I can’t say for certain. I don’t have sufficient privileges to register an Office 365 app, but there is a new app registration portal in preview that I may explore as I have time.

      1. FYI the Graph APIs are available as an OData feed, so provided your org isn’t too large, this should be a straightforward way to pull the data into Power BI

  4. Thank You so much for this help. its the only way we can fetch data in excel. can i use this code in the SQL part in rapidminer 5.3??

    1. Author

      Hello Sounak. I’m glad you found the post helpful. I am not familiar with Rapidminer, so unfortunately I cannot offer any help specifically with that product. If Rapidminer is able to work with REST APIs, then you could likely port it. You could also perhaps use another tool such as PowerShell or SSIS to obtain the data and store it in text files or SQL Server for further analysis.

  5. Hi Chris,

    Great article, it looks very promising.

    I filled in my ConsumerKey and ConsumerSecret, the syntax check is OK, but I get the following error message at the GetJson step: “Please specify how to connect…”

    Any suggestions are very welcome!

    Thanks, Edgar

    1. Author

      Hi Edgar, I don’t recall seeing that prompt when developing this query. Does the message appear in the PowerQuery data window? Or is there a popup? I ask because I’m wondering if the prompt is coming from PowerQuery or from the Twitter API.

      This is what my screen looks like on the GetJson step – link.

      1. HI Chris, The message appears in a yellow bar at the top of the main pane inside the Query Editor. I do not know how to send you a screenshot.
        Thanks, Edgar

  6. Nice article, greatly appreciated. I wanted to ask if you were able to find a solution for pulling Twitter Analytics (uses the Twitter AD API) with Power Query. The problem is that Twitter AD API does not work with OAuth2.0 that you use in this example.

    1. Author

      Hi James, I have not worked with the Twitter Analytics or Ads API at all. I looked over the documentation, and it appears that the token for these requests must represent the “current user”. This authentication method is more difficult than the “Application-Only” authentication in my example above, but I think it’s still possible with Power Query. Part of this authentication request involves generating an oauth nonce, which is essentially a random alphanumeric string. I’ve developed some M code that generates a random alphanumeric string, and I’ll be posting it soon. It’s not a complete solution for the Analytics API, but it’s one piece of the puzzle.

      1. Chris, yes that authentication is more challenging. It would be easier working with a web application because the POST requires an actual URL for the redirect for a user to login thereby confirming the “current user” you mention above. The issue here is that with Power Query, I don’t find think a solution requiring browser redirect from Power Query is a feasible solution. All other major social media outlets work with OAUTH2. Not sure why Twitter took this path.

  7. Hello, i got the error: Expression.Error: The ‘Authorization’ header is only supported when connecting anonymously. These headers can be used with all authentication types: Accept, Accept-Charset, Accept-Language, Cache-Control, If-Modified-Since, Referer

    Do you know why?

    1. Author

      Phylipe, doing a HTTP POST in Power Query is only supported when using anonymous authentication. Otherwise you would have redundant or conflicting credentials. When you first run the query in this blog post, Power Query should ask you how you want to connect. This is where you select the anonymous option. If you need to change the credentials, you can do so via the Data Source Setting menu in the Excel/Power Query menu.


  8. This sample saved my day – but for a different data source. I was struggling to pull data from our cloud based ticket system provided by Zendesk – but some minor tweaks in the query from the article and my face was one big smile.

    1. Author

      That’s great to hear! Thanks for the feedback!

  9. Thank you for this awesome functionality. I was wondering if you could provide some input on the following: When I publish a dashboard that pulls in tweets as you have described here, it breaks the refresh ability. If I try to manually refresh the data set in Power BI service, it says that my data source credentials are invalid. The errors are asking me to edit my credentials for:

    https://api.twitter.com/oauth2/token

    https://api.twitter.com/1.1/search/tweets.json?q=%5BMy Query]&count=100

    Any thoughts?

    1. Author

      David, I have experienced a similar error with a REST call in Power BI. Some have claimed to have solved this by using the RelativePath argument with Web.Contents. Unfortunately I have not had a chance to try this myself. The forum post is here. Chris Webb has written a blog post about RelativePath that is worth reading as well.

  10. Hey Chris, awesome post. I am in the field of architecture and just gradually getting in to data science. So this is new terrain to me. Is there a way to use the same logic to get search data from instagram?

    Thanks.

    1. Author

      Thank you! Yes, Instagram does have an API. I have not had an opportunity to explore it, myself. Here’s a link – Instagram API.

  11. Hi, Chris, i changed code for OAuth to:
    authKey = “Basic ” & Binary.ToText(Text.ToBinary(“:”),0),
    url = “https://api.twitter.com”,

    GetJson = Web.Contents(url,
    [
    Headers = [#”Authorization”=authKey,
    #”Content-Type”=”application/x-www-form-urlencoded;charset=UTF-8″],

    ),

    But i responopsed the same error ” “Please specify how to connect…”.

    What i did wrong? Thank you!

    1. Something strange with code, sorry.

  12. DataSource.Error: Web.Contents with the Content option is only supported when connecting anonymously.
    Details:
    DataSourceKind=Web
    DataSourcePath=https://api.twitter.com/oauth2/token

    1. I’m getting the same error how can correct it????

      1. Author

        Make sure that the Power Query connections for both the token and the Search Tweets URLs are set to use Anonymous access. You can change the credentials in the Data Source Setting menu in the Excel/Power Query menu.

        In Data Source Settings, select the data source, then Edit Permissions. Then under Credentials, select Edit and change to Anonymous.

  13. Is there any way to get more than 100 tweets? I’ve seen other apps without that restriction.

      1. Is this still a limitation and this is the only workaround ?

        1. Author

          Yes, this method still has a maximum of 100 results per query. Twitter has an article called Working with Timelines that discusses how to page through results.

      2. Can you help how can i get more than 200 rows ??i m not getting how to work with the suggestion at stack overflow whch you mention above

        1. Author

          Getting more than 100 rows requires using a technique called cursoring. You would use a query similar to the one in this post, but you supply additional parameters to specify the range of data to return. I think this is possible with Power Query, but I have not tried it myself, and it was outside the scope of this post.

          Twitter has an article called Working with Timelines that discusses this technique.

Leave a Reply (Markdown formatting available)