Thursday, November 13, 2014

How to add a Twitter feed to your App Studio app

I've recently released an app for the UK based charity Toilet Twinning. I wholeheartedly encourage you to support the work they do and twin your toilet now. This post is based on lessons from building that app.

The app started life in App Studio but I wanted to add the ability to show tweets from a specific account in the app. I was surprised that this functionality wasn't built in. I was further surprised that there wasn't good documentation anywhere to help add it.
This post is an attempt to address this and show how to add a twitter feed to an app studio app,


If you've looked into trying to do this before you may have discovered that there is a video recorded as Build that shows a way of doing this. You'll find it at http://channel9.msdn.com/Events/Build/2014/2-565 The downside of this is that it is more complicated than I needed and only a small amount of the code is available (at
http://blogs.msdn.com/b/rido/archive/2014/04/25/app-studio-build-session.aspx).


The following uses no third party libraries and is done entirely in Visual Studio. It allows adding a list of tweets by a named user to be displayed in the app without requiring a user to log in. If you wanted it to display a search term, such as a hashtag, then minor modifications would be needed but they aren't documented here.

The twitter API requires that all requests are from an authenticated source. While you may typically have a user authenticate, in this case the app is the what is authenticated, using Application-only authentication. This provides access to limited API methods but is sufficient for our needs. If you wish to know more see https://dev.twitter.com/oauth/application-only

To integrate with Twitter you first need to create an app entry on the Twitter website.

  • Go to https://apps.twitter.com/
  • then click "Create New App"
  • You can add any website value if you don't a suitable URL right now. Just remember to come back and replace it with the Store link later.
  • You don't need to enter a callback URL.
  • After accepting the terms and creating the app entry, go to the "Keys and Access Tokens" tab.

This is where you'll get the values for the "Consumer Key" and "Consumer Secret". You'll need to add these to TwitterDataSource.cs later.
Leave the permissions as "Read-only".

Please note that the values in this image are no longer valid for anything. Don't waste your time trying to check for security holes. They are for an app entry that no longer exists and won't work. ;)


We now need to modify and add some files to the solution. You'll find all the changes at https://gist.github.com/mrlacey/1cd1f7a0049c69eb774f but we'll also step through all the individual changes.


To add the details to both the Windows and Windows Phone project we need to add or modify 10 files:

1. Firstly we need a ViewModel to handle the retrieval of data and passing it to the view. Create AppStudio.Shared\Views\TwitterViewModel.cs (from here).

2. The ViewModel has to get the data from somewhere so next we need get the data, so we a data source.  Create AppStudio.Data\DataSources\TwitterDataSource.cs (from here)
It is in this file where we need to set the three constants that are used. The first is the username of the account you want to show the tweets from. The next two are the application API values highlighted above.


3. The data source needs something that will provide it with the data from the twitter API. Create AppStudio.Data\DataProviders\TwitterDataProvider.cs (from here)

4. The provider will return a collection of strongly typed objects. So we need to define the schema of that object. Create AppStudio.Data\DataSchemas\TwitterSchema.cs (from here)

5. Now we've got the data back from Twitter and into our ViewModel we need to tell our MainViewModel about it. Update AppStudio.Shared\ViewModels\MainViewModel.cs with the instructions here.

6. We now need to think about how the data will be displayed. We'll be using resources to define the templates we use in the views. We need to add a reference to the shared app resource (AppStudio.Shared\App.xaml) as defined here.

Let's update the Windows Phone project first.

7. We need to add a new hub section to the main page (AppStudio.WindowsPhone\Views\MainPage.xaml) that will look like this.

8. Then we need to add the resources we defined in the shared project and use in the new hub section. Create AppStudio.WindowsPhone\Views\DataTemplates\TwitterViews.xaml (from here)

Now let's update the Windows project.

9. Again, we need to add a new hub section to the main page (AppStudio.Windows\Views\MainPage.xaml) that will look like this.

10. Finally we need to add the data templates used by the view. Create AppStudio.Windows\Views\DataTemplates\TwitterViews.xaml (from here)


And with that we're done.



An important point to note is that when submitting your app to the store and using the above code, there is an extra consideration you need when submitting the Windows version.
In the cryptography section there are specific declarations you must make as credentials are encrypted and passed over SSL to authenticate with the Twitter API.

Assuming you're not doing anything else involving encryption then you just need to select "Yes" for both questions. Check the box if you can confirm the distributability of your app as asked. If you have other encryption in your app or questions about the legal consequences of using encryption in your app then seek appropriate legal advice.

An important note if you want to update this code and change what or how it's displayed.
As part of the terms and conditions of using twitter and integrating it within your app there are specific requirements about how you display a tweet. You'll find these at https://dev.twitter.com/overview/terms/display-requirements. Consider these carefully before you decide to modify how individual tweets are displayed.


Additionally, if you're interested in improving App Studio apps in other ways you should check out this related blog post.


11 comments:

  1. Even added difficult to ascertain are what is frequently referred to as "frankenwatches." These timepieces,which cover Citizens,are accumulated from apparatus of different watch brands.The Rolex Oyster Perpetual is a archetypal watch omega replica that never goes out of style.This is a time section that has been to the acme of Mount Everest,sunk into the Pacific Ocean and broiled to 500 degrees F,and it still looked and performed great.Retail amount for a Rolex is $5,000 to $10,000,so bethink the adage,"You get what you pay for" if your acquaintance tells you he can get you one for louis vuitton replica just $1,000.One of the a lot of affecting realities about online replica handbags is that they are alleged as the affluence artist replica handbags.Replica handbags or actor handbags appeal is ascent these canicule maybe because the abridgement is still down or there are abounding barter that do not wish to gucci replica absorb their money on cher affluence artist brands.Today,these kinds of handbags can be begin just about anywhere there are even accessible in beating off food and arrangement stores.Surely,replica handbags attending like the 18-carat designs of affluence accoutrements that they wish to prada replica archetype or imitate.The decidedly cheaper amount of actor accoutrements is usually one of the affidavit why there are abounding humans abnormally the women buy them.Anyway,some replica handbags are superior backpack and a lot of of them are able-bodied fabricated and actor the attending of the branded ones.

    ReplyDelete
  2. Finally, if you require Triple a score high end Prada carriers for exploring, you need to locate a efficient supplier. Presently, most people opt to Omega Replica uk purchase carriers from on-line outlets. There are a lot of advantages for those to order Prada carriers from on-line outlets. Firstly, getting identical from on-line outlets is easy. Your Cartier Repilica Watches will be delivered to your home only after a few presses of the mouse button. The second thing is, internet vendors supply you with the best Omega Replica competitive prices. Finally, online outlets normally have the most significant variety of carriers to provide your Breitling Replica flavour and funds. Fourthly, remember to read through fashion magazines to know what have been in design and style, which assists you stay way from http://scotss.org.uk/pages/index.htm remaining embarrassed about out-of-time developments and style.

    ReplyDelete
  3. The twitter API requires that all requests are from an authenticated source. While you may typically have a user authenticate, rap beats for sale

    ReplyDelete
  4. Windows apps are quite inn these days.. as the trend is changing now and this sounds interesting..

    ReplyDelete
  5. Add some ice cream prepared mix again proudly told me not being collected cream, use a mix of goods instead of international brands facebook entrar , facebook entrar , facebook entrar , girl.go.games , facebook entrar perfil , facebook entrar mi cuenta , facebook entrar login

    ReplyDelete
  6. A great possibility for me and it was a superb knowledge to view this site. Very difficult to uncover these beneficial web page or web site. I have many devices and achieving proper picture of these worked well and energy continues to be seeing about this weblog. Often my own intend to make my personal site as well as my own enjoyment is growing due to this page. I we do hope you may well be more effective.
    happy wheels| cool math games| 8 ball pool| sudoku| yoob| friv| monster high
    tetris| shooting games| barbie games| friv4school|

    ReplyDelete
  7. chicago bears, http://www.chicagobearsjerseys.us/
    nobis outlet, http://www.wellensteyn.com.co/
    tory burch shoes, http://www.toryburchoutletonline.in.net/
    replica watches, http://www.replicawatchesforsale.us.com/
    ed hardy shoes,ed hardy,ed hardy clothing,ed hardy shoes for women,ed hardy clothing for men,ed hardy perfume,ed hardy watches
    christian louboutin, http://www.scarpelouboutin.it/
    ugg outlet store, http://www.uggboots-outlet.co.uk/
    nike huarache, http://www.nike-airhuarache.co.uk/
    nike air max, http://www.airmax2015.in.net/
    ray ban, http://www.occhiali-rayban.it/
    louis vuitton bags on sale, http://www.louisvuittonbag.us.com/
    nike air max,nike roshe,nike huarache
    canada goose outlet, http://www.canadagoose-coats.us.com/
    hermes belt for men, http://www.hermesbelts.us/
    north face jackets, http://www.thenorthfaceclearances.us.com/
    ray ban sunglasses,ray ban sunglasses outlet,ray ban,rayban,ray bans,ray ban outlet,ray-ban,raybans,ray ban wayfarer,ray-ban sunglasses,raybans.com,rayban sunglasses,cheap ray ban
    cheap oakley sunglasses, http://www.oakleysunglassesdiscount.us.com/
    woolrich clothing, http://www.canadagoosejackets.in.net/
    kkkk10 13

    ReplyDelete
  8. Your information is good. Twitter users can use many diverse applications developed by third parties, including photo-sharing website and other linked to his Twitter account.!

    Dora , Kizi new , G9G , 4 Games , 85 jogos , Friv 4 , kizi 2 , Huz , 85 Play

    ReplyDelete