Tuesday, 11 June 2013

New Twitter API Version 1.1 (Part 1: Authentication)

As you know, the old twitter API V1 has been retired few hours ago [@twitterapi]. What does it mean for SharePoint developers? Well if you use any twitter webpart which using the API V1, then it is dead by now, because twitter depricate the API V1 and you need to upgrade your apps to use the new twitter API V1.1.
 Figure 1 - Twitter TimeLine Web part using API V1 (Before 11/06/2013)

Figure 2 - Twitter TimeLine Web part using API V1 (After 11/06/2013)

I have been trying to upgrade my company’s twitter web part to version 1.1, and find out there is very hard to find the relevant information from many resources out there. Finally after spending two days searching and trialing different solutions, I could do it, so here is the summary of what I found usefull, and a quick steps guide of what exactly needs to be done to build a reader webpart for twitter  timelines.
To use the Twitter API V 1.1, you must first authenticate (source), even if you only want to read timelines like in my case. After authenticate is done, you simply call the API and get the results in Json, and now you can display those data in whatever format you like to do it.
There aren't quick and easy instructions on how to move from that model because there aren't quick and easy ways to migrate. One has to completely rethink their approach to the API in this model, leveraging a server-to-server approach instead of a client-to-server model. Instead of using jQuery to make requests to the API, you'll need to make requests using OAuth from your back end. (Source from @episod)

Authentication (OAuth FAQ):

Before start coding, you need to create an application. https://dev.twitter.com/apps.
Once you've done that, you need to get your consumer key, consumer secret, access token and access token secret.




Here is a link to the twitter document for Moving from Basic Auth to OAuth. After many searching and reading, here is the best approach for my case to do the oAuth in SharePoint.
Thanks to @countscar and @bobbykc13 who post their codes here. this is what I did to build this code for SharePoint 2010.

To use/test this code, create a new web application in C# and copy this code to the Page Load function. Replace the "consumerKey, consumerSecret, accessToken, accessTokenSecret" with your own key details and run the application.

            // oauth application keys
            var consumerKey = "Your Key";
            var consumerSecret = "Your Key";
            var accessToken = "Your Key";//oauth_token
            var accessTokenSecret = "Your Key";//oauth_token_secret
            var Count = "3";

            // oauth implementation details
            var oauthSignatureMethod = "HMAC-SHA1";
            var oauthVersion = "1.0";
            // message api details
            //var status = "Updating status via REST API if this works";
            var resource_url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
            var screen_name = "TransPerth";

            // unique request details
            var oauth_nonce = Convert.ToBase64String(
               new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
            var timeSpan = DateTime.UtcNow
                - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
            var oauth_timestamp = Convert.ToInt64(timeSpan.TotalSeconds).ToString();

            // create oauth signature
            var baseFormat = "count={7}&oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                            "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&screen_name={6}";
            var baseString = string.Format(baseFormat,
                                        consumerKey,
                                        oauth_nonce,
                                        oauthSignatureMethod,
                                        oauth_timestamp,
                                        accessToken,
                                        oauthVersion,
                                         Uri.EscapeDataString(screen_name),
                                         Uri.EscapeDataString(Count)
                                        );

            baseString = string.Concat("GET&", Uri.EscapeDataString(resource_url), "&", Uri.EscapeDataString(baseString));

            var compositeKey = string.Concat(Uri.EscapeDataString(consumerSecret),
                                    "&", Uri.EscapeDataString(accessTokenSecret));

            string oauth_signature;
            using (HMACSHA1 hasher = new HMACSHA1(ASCIIEncoding.ASCII.GetBytes(compositeKey)))
            {
                oauth_signature = Convert.ToBase64String(
                    hasher.ComputeHash(ASCIIEncoding.ASCII.GetBytes(baseString)));
            }

            // create the request header
            var headerFormat = "OAuth oauth_nonce=\"{0}\", oauth_signature_method=\"{1}\", " +
                               "oauth_timestamp=\"{2}\", oauth_consumer_key=\"{3}\", " +
                               "oauth_token=\"{4}\", oauth_signature=\"{5}\", " +
                               "oauth_version=\"{6}\"";

            var authHeader = string.Format(headerFormat,
                                    Uri.EscapeDataString(oauth_nonce),
                                    Uri.EscapeDataString(oauthSignatureMethod),
                                    Uri.EscapeDataString(oauth_timestamp),
                                    Uri.EscapeDataString(consumerKey),
                                    Uri.EscapeDataString(accessToken),
                                    Uri.EscapeDataString(oauth_signature),
                                    Uri.EscapeDataString(oauthVersion)
                            );

            // make the request
            ServicePointManager.Expect100Continue = false;

            var postBody = string.Format("screen_name={0}&count={1}", Uri.EscapeDataString(screen_name), Uri.EscapeDataString(Count));
            resource_url += "?" + postBody;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
            request.Headers.Add("Authorization", authHeader);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";

            WebResponse response = request.GetResponse();
            string responseData = new StreamReader(response.GetResponseStream()).ReadToEnd();
            //Label1.Text = responseData;
  
I will write another post soon, to explain the display method, and how convert the JSON results to a repeater and make the SharePoint web part.

Tuesday, 28 May 2013

mstsc - Remote Desktop Connection

mstsc.exe [<Connection File>] [/v:<Server>[:<Port>]] [/admin] [/f] [/w:<Width> /h:<Height>] [/public] [/span] [/?]
mstsc /v: /admin /f /span
mstsc /v: /admin /w:800 /h:600 /public

Parameter
Description
<Connection File>
Specifies the name of an .rdp file for the connection.
/v:<Server[:<Port>]
Specifies the remote computer and, optionally, the port number to which you want to connect.
/admin
Connects you to a session for administering the server.
/f
Starts Remote Desktop Connection in full-screen mode.
/w:<Width>
Specifies the width of the Remote Desktop window.
/h:<Height>
Specifies the height of the Remote Desktop window.
/public
Runs Remote Desktop in public mode. In public mode, passwords and bitmaps are not cached.
/span
Matches the Remote Desktop width and height with the local virtual desktop, spanning   across multiple monitors if necessary.
/edit <Connection File>
Opens the specified .rdp file for editing.
/migrate
Migrates legacy connection files that were created with Client Connection Manager to new .rdp connection files.
/?
Displays help at the command prompt.


Wednesday, 1 May 2013

Sydney SharePoint Conference


The conference was very fruitful for me and I think many are agree that a great job was done very well.
I learnt many things and coming back to work I started applying them right away.
Because my work SharePoint environment is still 2007/2010, I cannot do anything about the new 2013 things but still there is lots of room for doing things better in my current farms.
So, I have a new plan to redo my company's whole intranet which is 2010, so starting from scratch; I decided to do the installation and whole deployment by script. I will post what I learn ​​​for future reference.

Monday, 1 October 2012

The Excel Workbook Cannot be Opened

This could be because you use a custom Service Account for all your service application / Excel Services then you need to do either one of the following :
  • Add the Excel services Service account to be a Dbowner on your content database that holds the web application
  • Or copy the following script and run it

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA 0
$webApp = Get-SPWebApplication "VDM_Intranet"
$webApp.GrantAccessToProcessIdentity("SVC-SP-ServiceApps")
"Completed"
Source:
http://blogs.msdn.com/b/jjameson/archive/2010/05/04/the-workbook-cannot-be-opened-error-with-sharepoint-server-2010-and-tfs-2010.aspx

Thursday, 7 June 2012

JQuerry & SharePoint 2010 (Part 2)

Following my Previous post, I decided to put the JQuerry library in my Style Library.
Remember: this is not a one off task, as later on you may need to add more scripts in your scripts library.
The next step will be to add a JQuerry reference to your Master Page.It will allow you to use your JQuerry items in every page which is using that Master Page whithout having to reference to it every time. and remember, you only can have 1 JQuerry reference at a time.


By default SharePoint 2010 uses v4.master as sites default and custom master page.
You probably want to create a copy of V4 and add the JQuerry reference in the custom one. When you created your custom master page, open it in SharePoint designer to be able to edit it. Find the file, Right click on it and select "Edit in advanced mode".

Add "ScriptLink" tag as bellow inside the <Head> tag:
<script src="/Style%20Library/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
Save the master page, check in and publish it.

Now go to your site setting, and change the site master page from V4 to your newly created custom master page. and that's it you can start using JQuerry and add your own custom codes. have fun.


In the next post, we are going to add some cool features to this newly build SharePoint site.