Sunday, November 9, 2014

Getting started with Egnyte API

Now the first part is getting the token !
so I wrote a short code on the custom button to get the code and then redirect it to a page which calls a page and its controller which saves it in custom settings.


Here is the diagram explaining the same !!

You can read about it here <Token Page>
Custom Button
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/24.0/apex.js")} 

var qResult = sforce.connection.query("Select Name, Token__c from eToken__c ");
records = qResult.getArray("records");
if(records == ''){
var idofQuote ='{!Quotes__c.Id}';
window.location.href='https://<ur domain>.egnyte.com/puboauth/token?client_id=<ur api KEY>&redirect_uri=<complete url of ur vf page>?id='+idofQuote+'&mobile=0';
}

The Page
    
Redirecting............  
  
       
      
          
      
  
  
  
  


The part of the controller
public string authToken {get;set;}
public pagereference basicAuthCallout(){
        id quoId = ApexPages.currentPage().getParameters().get('Id');
        
        string token2 ; 
        
        
            // Get the URL for the current request.
            url currentRequestURL = URL.getCurrentRequestUrl();
            system.debug('>>>>>>>>>>authToken>>>>>>>>>>>>>'+authToken);
     if(authToken!= null){  
             String extract = authToken ;
            list newstring2 = new list();
            list newstring;
            if(extract !=null){
                newstring = new list(extract.split('#access_token='));
                
            system.debug('*******newstring******'+newstring);}
            
            for(String a : newstring){
                if(a.contains('&token_type')){
                    newstring2 = a.split('&token_type');
                }
            }
            if(newstring2 !=null && newstring2.size() > 0){
                string theToken = newstring2[0];
                system.debug('theToken'+theToken);
                token2 = theToken;
            }
            
    }
            eToken__c customToken = new eToken__c();
            customToken.name = UserInfo.getUserEmail();
            customToken.Token__c = token2;
            if(([Select Name, Token__c from eToken__c]).isEmpty()){
            	upsert customToken ;
            }	
            PageReference pageRef = new PageReference('/'+ quoId); 
            return pageRef;
            }
  

No comments:

Post a Comment