Getting Munchkin API associateLead call to work SHA1 hash issues

Anonymous
Not applicable

Getting Munchkin API associateLead call to work SHA1 hash issues

Hi all,

I've been following the instructions and discussions within the community to write the associateLead call from the Munchkin Javascript API. However I'm having trouble generating the SHA1 hash... Could anyone help, please? I'm using ASP.NET.

Here's the instructions I've been following (About half way down the page): http://community.marketo.com/MarketoArticle?id=kA050000000Kyr7CAC

The specific problem I'm having is debugging the script that generates the SHA1 hash - I know that the AssociateLead function works itself as i've tried hardcoding an email address and generating a SHA1 hash using a web app generator.

Once I know the SHA1 generation code is working, I then need to figure out how to pass that value/string into the AssociateLead function.

All help appriciated! Thank you! 



Here's my code so far:

<script src="http://munchkin.marketo.net/munchkin.js" type="text/javascript"></script>
<script>
 mktoMunchkin("removed");
 mktoMunchkinFunction('associateLead',
  {
Email: '@formModel.Email',
FirstName: '@formModel.FirstName',
LastName: '@formModel.Surname',
Company: '@formModel.Company',
Title: '@formModel.Position',
Phone: '@formModel.Telephone',
Address: '@formModel.CompanyAddress1',
City: '@formModel.CompanyTownCity',
PostalCode: '@formModel.CompanyPostcode',
Country: '@formModel.Country',
Industry: '@formModel.Industry',
Solution_interest__c: '@formModel.Solution',
Purchase_authority__c: '@formModel.Authority',
Key_business_challenges__c: '@formModel.Objectives',
Unsubscribed: '@formModel.NewsLetter'},
'HASHHERE');   
 

and the recommended code for dynamically generating the SHA1 (added email variable)...
 
 
string GetEncryptedEmail(string emailAddress) { <-- Do I need to change this?   -->
string encryptedEmail = "";
 
string apiKeyEmail = 'PrivateKeyRemoved' + '@formModel.Email';
byte[] input = Encoding.UTF8.GetBytes(apiKeyEmail);
byte[] hash = null;
 
SHA1 sha = new SHA1CryptoServiceProvider();
hash = sha.ComputeHash(input);
encryptedEmail = BitConverter.ToString(hash).Replace("-", "");
 
return encryptedEmail.ToLower();
 
}
 
</script>
Tags (1)
1 REPLY 1
Anonymous
Not applicable

Re: Getting Munchkin API associateLead call to work SHA1 hash issues

Here's what we use with ASP.NET code (webforms, not MVC) and is based on Marketo's recommendations. The code you posted appears correct but maybe this will help

In the example, assume that userEmail looks like "alpha@beta.com"

Client:
<script>
// other Marketo initialization, etc is done here
var munchkinHash = '<%= this.GetMunchkinHash() %>';
mktoMunchkinFunction('associateLead', { Email: userEmail, KeyPositionTitle: selectedTitle }, munchkinHash);
</script>

Server:
        public static string GetMunchkinHash(string userEmail)
        {
            if (!string.IsNullOrEmpty(userEmail))
            {
                // Calculate hash
                // From http://developers.marketo.com/documentation/websites/lead-tracking-munchkin-js/ :
                //  The security hash is encoded with SHA1.
                //  To generate this, concatenate your API Private Key with the lead's email address, 
                //  then encode it with SHA1 (the non-HMAC version).
                //  Make sure [SHA1 string is] in lowercase so it gets processed correctly.
 
                string marketoApiKey = Project.Config.Default.GetConfigString("Marketo", "MarketoApiKey", "");
                string hash = CalculateSHA1(marketoApiKey + userEmail);
                return hash;
            }
            else
            {
                return string.Empty;
            }
        }
 
        private static string CalculateSHA1(string text)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(text);
            SHA1CryptoServiceProvider cryptoTransformSHA1 = new SHA1CryptoServiceProvider();
            return BitConverter.ToString(cryptoTransformSHA1.ComputeHash(buffer)).Replace("-", "").ToLower();
        }