Sunday, September 13, 2015

Twitter Integration with CRM 2013 and .NET applications

Microsoft has launched the Microsoft Dynamics CRM Sample Social Care Application together with some new entities that they are purposely created for the Social Live and Connection, during the launching of the Microsoft Dynamics CRM Springwave ‘14 Updates.  I tried integrating twitter with CRM using below blogs but faced many issues. 

http://www.microsoft.com/en-us/download/details.aspx?id=43122

http://missdynamicscrm.blogspot.sg/2014/07/run-social-care-application-crm-2013.html.


I got error while executing the app( Value was either too large or too small for an Int32)

Solution i found  in below blogs.
http://stackoverflow.com/questions/21138289/tweetsharp-getaccesstoken-method-returns-null-but-only-sometimes
http://www.nuget.org/packages/TweetSharp-Unofficial/

 Thorough of understanding the integration  between .NET and twitter. is as below 

https://github.com/danielcrenna/tweetsharp


Also i tried integrating using tweetizzer.

https://github.com/detroitpro/Twitterizer/tree/master/Twitterizer.OAuth



Below application is .NET integration with twitter without any wrapper.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Security.Cryptography;
using System.IO;

namespace TwitterOAuth
{
    class Program
    {
        static void Main(string[] args)
        {
            // oauth application keys
            var oauth_token = "xxxxxxxxx";
            var oauth_token_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            var oauth_consumer_key = "xxxxxxxxxxxxxxxx";
            var oauth_consumer_secret = "xxxxxxxxxxxxxxxxxxxxxxxx";

            // oauth implementation details
            var oauth_version = "1.0";
            var oauth_signature_method = "HMAC-SHA1";

            // 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();

            // message api details
            var status = "Updating status via REST API if this works again";
            var resource_url = "https://api.twitter.com/1.1/statuses/update.json";

            // create oauth signature
            var baseFormat = "oauth_consumer_key={0}&oauth_nonce={1}&oauth_signature_method={2}" +
                            "&oauth_timestamp={3}&oauth_token={4}&oauth_version={5}&status={6}";

            var baseString = string.Format(baseFormat,
                                        oauth_consumer_key,
                                        oauth_nonce,
                                        oauth_signature_method,
                                        oauth_timestamp,
                                        oauth_token,
                                        oauth_version,
                                        Uri.EscapeDataString(status)
                                        );

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

            var compositeKey = string.Concat(Uri.EscapeDataString(oauth_consumer_secret),
                                    "&", Uri.EscapeDataString(oauth_token_secret));

            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(oauth_signature_method),
                                    Uri.EscapeDataString(oauth_timestamp),
                                    Uri.EscapeDataString(oauth_consumer_key),
                                    Uri.EscapeDataString(oauth_token),
                                    Uri.EscapeDataString(oauth_signature),
                                    Uri.EscapeDataString(oauth_version)
                            );


            // make the request
            var postBody = "status=" + Uri.EscapeDataString(status);

            ServicePointManager.Expect100Continue = false;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(resource_url);
            request.Headers.Add("Authorization", authHeader);
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            using (Stream stream = request.GetRequestStream())
            {
                byte[] content = ASCIIEncoding.ASCII.GetBytes(postBody);
                stream.Write(content, 0, content.Length);
            }
            WebResponse response = request.GetResponse();
        }
    }
}




Twittter  Developer reference is below-


https://dev.twitter.com/oauth/overview/single-user

Also we have samples to explain integration using  tweetsharp  as below-

http://www.codeproject.com/Articles/584666/Twitter-Client-Login-to-Csharp-application-using-t

also we need to check whether our sample is referring to

https://api.twitter.com/1.1/ -  new

or
https://api.twitter.com/1/- old