1) Create an APP in Twitter. 2) Provide callback url in Twitter where twitter will send a code for authentication.(https://Your instance/apex/TwitterPage) 3) After creating a App in Twitter you will get Client Key and Secreat Key which will be used in Salesforce to send a reqest to twitter for authentication. 4) In salesforce you need to provide api end point of twitter in Remote Site setting first. 5) Now you need create a page in salesforce. Apex PAge -> TwitterPage
public class TwitterController
{
//Fetched from URL
String code ;
public TwitterController ()
{
code = ApexPages.currentPage().getParameters().get('code') ;
//Get the access token once we have code
if(code != '' && code != null)
{
AccessToken() ;
}
}
public PageReference TwitterAuth()
{
//Authenticating
PageReference pg = new PageReference('https:// Please_PUT HERE TwitterAuthorizeURL ?response_type=code&client_id=Put client id&redirect_uri=YOUR CALL BACK URL') ;
return pg ;
}
public void AccessToken()
{
//Getting access token from dropbox
String tokenuri = 'https://PLEASE PRovide api hit url of twitter?grant_type=authorization_code&code='+code+'&redirect_uri=YOUR CALL BACK URL';
HttpRequest req = new HttpRequest();
req.setEndpoint(tokenuri);
req.setMethod('POST');
req.setTimeout(60*1000);
Blob headerValue = Blob.valueOf('vaabb5qz4jv28t5' + ':' + 'dpmmll522bep6pt');
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
Http h = new Http();
String resp;
HttpResponse res = h.send(req);
resp = res.getBody();
System.debug(' You can parse the response to get the access token ::: ' + resp);
}
}
You need to go throught the Articals and then need to implement.
I also wanted to integrate twitter with salesforce.And i was trying the above code. however i was not able to do that .Please guide me and let me know where i am wrong?
Here is the code. public class TwitterController1 { //Fetched from URL String code ;
public TwitterController1 () { code = ApexPages.currentPage().getParameters().get('code') ; //Get the access token once we have code if(code != '' && code != null) { AccessToken() ; } }
public PageReference TwitterAuth() { //Authenticating PageReference pg = new PageReference('https://api.twitter.com/oauth2/token?response_type=code&client_id=eGUoMQPlhp96GVRotc2URnkVn&redirect_uri=https://c.ap2.visual.force.com/apex/TwitterController') ; return pg ; }
public void AccessToken() { //Getting access token from dropbox String tokenuri = 'https://api.twitter.com/1.1/statuses/mentions_timeline.json?grant_type=authorization_code&code='+code+'&redirect_uri=https://c.ap2.visual.force.com/apex/TwitterController'; HttpRequest req = new HttpRequest(); req.setEndpoint(tokenuri); req.setMethod('POST'); req.setTimeout(60*1000);
1) Create an APP in Twitter.
2) Provide callback url in Twitter where twitter will send a code for authentication.(https://Your instance/apex/TwitterPage)
3) After creating a App in Twitter you will get Client Key and Secreat Key which will be used in Salesforce to send a reqest to twitter for authentication.
4) In salesforce you need to provide api end point of twitter in Remote Site setting first.
5) Now you need create a page in salesforce. Apex PAge -> TwitterPage
You need to go throught the Articals and then need to implement.
-Thanks
Ashlekh Gera
I also wanted to integrate twitter with salesforce.And i was trying the above code.
however i was not able to do that .Please guide me and let me know where i am wrong?
Here is the code.
public class TwitterController1
{
//Fetched from URL
String code ;
public TwitterController1 ()
{
code = ApexPages.currentPage().getParameters().get('code') ;
//Get the access token once we have code
if(code != '' && code != null)
{
AccessToken() ;
}
}
public PageReference TwitterAuth()
{
//Authenticating
PageReference pg = new PageReference('https://api.twitter.com/oauth2/token?response_type=code&client_id=eGUoMQPlhp96GVRotc2URnkVn&redirect_uri=https://c.ap2.visual.force.com/apex/TwitterController') ;
return pg ;
}
public void AccessToken()
{
//Getting access token from dropbox
String tokenuri = 'https://api.twitter.com/1.1/statuses/mentions_timeline.json?grant_type=authorization_code&code='+code+'&redirect_uri=https://c.ap2.visual.force.com/apex/TwitterController';
HttpRequest req = new HttpRequest();
req.setEndpoint(tokenuri);
req.setMethod('POST');
req.setTimeout(60*1000);
Blob headerValue = Blob.valueOf('eGUoMQPlhp96GVRotc2URnkVn' + ':' + 'CBhyr5OvzpyXLpvmge2DhaHH6VoDF9ix1b5RF6rWB2nGPV2P3X');
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
Http h = new Http();
String resp;
HttpResponse res = h.send(req);
resp = res.getBody();
System.debug(' You can parse the response to get the access token ::: ' + resp);
}
}
i replaced all the values but it got stuck while authenticating.Am making any mistake.Please let me know.
Thanks
Rohini