• Vladimir Gorelik
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
I have some code generating OAuth signature and sending request to NetSuite.
I reviewed all the parameters I send to NetSuite with their developers and they told me that I have some issue with oauth_signature.

I will really appreciate if you take a look and tell me what I am doing wrong.
 
public String timestamp {get; set;} //seconds since unix epoch
    public String nonce {get; set;} //random number for making request unique
    public Map<String, String> oauth_params {get; set;} //store oauth params for signature generation   
    public OAuthService__c ns_oauth	{get; set;} //oauth data for ns -> salesforce
    	
    public String GenerateSignature(String httpMethod) {
    	
    	ns_oauth = [select Consumer_Key__c, Consumer_Secret__c, Authorization_URL__c, Realm__c, 
    						   (select token__c, secret__c from Tokens__r)
    					  from OAuthService__c
    					 where Name = 'SalesForce' limit 1];
    		
    	timestamp = String.valueOf(DateTime.now().getTime()/1000); //seconds since unix epoch
    	nonce = String.valueOf(Crypto.getRandomLong()); //random number
    
    	oauth_params = new Map<String, String>();
    
    	if(ns_oauth != null) { //store parameters for signature creating
    		oauth_params.put('oauth_version', '1.0');
    		oauth_params.put('oauth_nonce', nonce);
    		oauth_params.put('oauth_timestamp', timestamp);
    		oauth_params.put('oauth_consumer_key', ns_oauth.Consumer_Key__c);
    		oauth_params.put('oauth_token', ns_oauth.Tokens__r[0].token__c);
    	}
    		
    	String sig = normalizeUrl(ns_oauth.Authorization_URL__c);  //signature starts with normalized url (with port number)
    	Blob sigMac; //hash of signature
    	
    	sig = httpMethod.toUpperCase() + '&' + EncodingUtil.urlEncode(sig, 'UTF-8');
    		
    	//sort parameters  before appending to signature 
    	List<string> sortParams = new List<string>(oauth_params.keySet());
    	sortParams.sort();
    	
    	//append all the params for signature
    	for(String param_key : oauth_params.keySet()) {
    		sig += '&' + param_key + '=' + oauth_params.get(param_key);
    	}
    
    	//compute HASH using SHA-1 algorithm
    	//where key is acombination of concumer secret and token secret
    	
    	String hash_key = ns_oauth.Consumer_Secret__c + '&' + ns_oauth.Tokens__r[0].secret__c;
    
    	sigMac = Crypto.generateMac('HmacSHA1', Blob.valueOf(sig), Blob.valueOf(hash_key));
    
    	return EncodingUtil.urlEncode(EncodingUtil.base64Encode(sigMac), 'UTF-8');
    
    }
I get in response following:
 
{"error" : {"code" : "INVALID_LOGIN_ATTEMPT", "message" : "Invalid login attempt."}}