You need to sign in to do that
Don't have an account?
Salesforce with twitter integration Failed to validate oauth signature and token
Hello,
Integrating salesforce with twitter through apex code am facing error like
"Failed to validate oauth signature and token"
Am passing twitter Oauth token, secretkey... in string format like
string Stringval='Oauth_callback="'+EncodingUtil.urlEncode('https://c.ap1.visual.force.com/apex/twitterForcepage';, 'UTF-8')+'",oauth_consumer_key="eIGzjmMGeclmpSzxmjnKhQ", oauth_nonce="3de317379683f90d4f80da8879472036", oauth_signature="fiyY68Llzy2yREjUeJw0o%2Fdqcns%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1365504459", oauth_token="354671694-MrReLR18pmpDZulKaRVZR3CifKpI9ZFo1XUofyJn", oauth_version="1.0"';
string bVal=EncodingUtil.Base64Encode(blob.valueof(Stringval));
String authorizationHeader = 'Bearer :' + bval;
req.setHeader('Authorization',bVal);
How to integrate salesforce with twitter through apex? how to authenticate with twitter? or acess responce from twitter?
Can anyone suggest my issue where it is problem?
Thank you
Hi,
Your string format not seems to be correct . String format must be in following format:
// EndPoint URL
String endpoint = 'https://api.twitter.com/oauth/request_token'
// Passing parameter and must be percent encode.
String params='oauth_callback='+EncodingUtil.urlEncode('https://c.ap1.visual.force.com/apex/twitterlogin' , 'UTF-8')+'&oauth_consumer_key=lHVBPYDxxxx2JASf9yyEA&oauth_nonce=0afa694efd3feee68ebc180f3858a9a3&oauth_signature='+oauth_signature+'&oauth_signature_method=HMAC-SHA1&oauth_timestamp='+oauth_timestamp+'&oauth_token='+oauth_token+'&oauth_version=1.0';
To authenticate follow following link: https://dev.twitter.com/docs/api/1.1
POST oauth/request_token is first step of initialization through apex.
Twittet uses OAuth 1.0 authentication flow. Which require correct signature to generate. Before signing every key must be percent encode. Format of string to sign must be:
String baseString = 'POST&'+EncodingUtil.urlEncode('https://api.twitter.com/oauth/request_token' , 'UTF-8')+'&'+EncodingUtil.urlEncode('oauthcallback=https://c.ap1.visual.force.com/apex/twitterlogin&oauth_consumer_key=lHVBPYDxxxx2JASf9yyEA&oauth_nonce='+oauth_nonce+'&oauth_signature_method=HMAC-SHA1&oauth_timestamp='+oauth_timestamp+'&oauth_token='+oauth_token+'&oauth_version=1.0' , 'UTF-8');
Signature key is combination of "Consumer sectret + access Secret" and must be joined with '&'. e.g.
signkey = consumer_secret+'&'+oauth_token_secret;
Signature can be generate by this approach:
Blob OAuth = Crypto.generateMac('hmacSHA1' , Blob.valueOf(baseString) , blob.valueOf(signkey));
oauth_signature = EncodingUtil.urlEncode(EncodingUtil.base64Encode(Oauth), 'UTF-8');
nonce is a unique string of 32 byte random data and must not be repeat in any of requests.
For any doubts please reply and if it resolves your problem please mark it as solution.
Thanks.