Wednesday 16, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Thursday, 14 July 2016

Salesforce OAuth 2.0 Example using Scribe Java

In this example we are going to see about how to use Salesforce OAuth 2.0 using Scribe Java

Salesforce OAuth 2.0 Example

SalesforceExample.java

01.import java.io.IOException;
02.import java.net.URLDecoder;
03.import java.net.URLEncoder;
04.import java.util.Scanner;
05. 
06.import com.github.scribejava.apis.SalesforceApi;
07.import com.github.scribejava.apis.salesforce.SalesforceToken;
08.import com.github.scribejava.core.builder.ServiceBuilder;
09.import com.github.scribejava.core.model.OAuthRequest;
10.import com.github.scribejava.core.model.Response;
11.import com.github.scribejava.core.model.Verb;
12.import com.github.scribejava.core.oauth.OAuth20Service;
13.import java.security.KeyManagementException;
14.import java.security.NoSuchAlgorithmException;
15. 
16.public abstract class SalesforceExample {
17. 
18.    private static final String NETWORK_NAME = "Salesforce";
19. 
20.    public static void main(String... args) throws IOException, NoSuchAlgorithmException, KeyManagementException {
21.        // Replace these with your client id and secret
22.        final String clientId = "your client id";
23.        final String clientSecret = "your client secret";
24.        //IT's important! Salesforce upper require TLS v1.1 or 1.2.
25.        //They are enabled in Java 8 by default, but not in Java 7
26.        SalesforceApi.initTLSv11orUpper();
27. 
28.        // The below used ServiceBuilder connects to login.salesforce.com
29.        // (production environment).
30.        //
31.        // When you plan to connect to a Sandbox environment you've to use SalesforceApi.sandbox() API instance
32.        // new ServiceBuilder.....build(SalesforceApi.sandbox());
33. 
34.        final OAuth20Service service = new ServiceBuilder()
35.                .apiKey(clientId)
36.                .apiSecret(clientSecret)
37.                .callback("http://ift.tt/29IjOEi")
38.                .build(SalesforceApi.instance());
39. 
40.        System.out.println("=== " + NETWORK_NAME + "'s OAuth20 Workflow ===");
41.        System.out.println();
42. 
43.        // Obtain the Authorization URL
44.        System.out.println("Fetching the Authorization URL...");
45.        final String authorizationUrl = service.getAuthorizationUrl();
46.        System.out.println("Got the Authorization URL!");
47.        System.out.println("Now go and authorize ScribeJava here:");
48.        System.out.println(authorizationUrl);
49.        System.out.println("And paste the authorization code here");
50.        System.out.print(">>");
51.        final String code;
52.        try (Scanner in = new Scanner(System.in)) {
53.            code = in.nextLine();
54.        }
55.        System.out.println();
56. 
57.        // The code needs to be URL decoded
58.        final String codeEncoded = URLDecoder.decode(code, "UTF-8");
59. 
60.        // Trade the Request Token and Verifier for the Access Token
61.        System.out.println("Trading the Request Token for an Access Token...");
62.        final SalesforceToken accessToken = (SalesforceToken) service.getAccessToken(codeEncoded);
63.        System.out.println("Got the Access Token!");
64.        System.out.println("(if your curious it looks like this: " + accessToken + ", 'rawResponse'='"
65.                + accessToken.getRawResponse() + "')");
66.        System.out.println();
67. 
68.        System.out.println("instance_url is: " + accessToken.getInstanceUrl());
69. 
70.        // Now let's go and ask for a protected resource!
71.        System.out.println("Now we're reading accounts from the Salesforce org (maxing them to 10).");
72. 
73.        // Sample SOQL statement
74.        final String queryEncoded = URLEncoder.encode("Select Id, Name from Account LIMIT 10", "UTF-8");
75. 
76.        // Building the query URI. We've parsed the instance URL from the accessToken request.
77.        final String url = accessToken.getInstanceUrl() + "/services/data/v36.0/query?q=" + queryEncoded;
78. 
79.        System.out.println();
80.        System.out.println("Full URL: " + url);
81. 
82.        final OAuthRequest request = new OAuthRequest(Verb.GET, url, service);
83.        request.addHeader("Authorization", "Bearer " + accessToken.getAccessToken());
84.        final Response response = request.send();
85.        System.out.println();
86.        System.out.println(response.getCode());
87.        System.out.println(response.getBody());
88.    }
89.}

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Flickr OAuth 1.0a Example using Scribe JavaIn this example we are going to see about how to use Flickr OAuth 1.0a using Scribe Java Flickr OAuth 1.0a Example using Scribe FlickrExample.java import java.util.Scanner; import com.github.scribejava.core.builder.ServiceBu… Read More
  • LinkedIn OAuth 1.0a with Scope Example using Scribe JavaIn this example we are going to see about how to use LinkedIn OAuth 1.0a using Scribe Java LinkedIn OAuth 1.0a Example with Scope LinkedInExampleWithScopes.java import java.util.Scanner; import com.github.scribejava.core.bui… Read More
  • AWeber OAuth 1.0a Example using Scribe JavaIn this example we are going to see about how to use AWeber OAuth 1.0a using Scribe Java AWeber OAuth 1.0a Example using Scribe AWeberExample.java import java.util.Scanner; import com.github.scribejava.core.builder.ServiceBu… Read More
  • Twiter OAuth 1.0a Example using Scribe JavaIn this example we are going to see about how to use Twitter OAuth 1.0a using Scribe Java Twitter OAuth 1.0a Example with Scope TwitterExample.java import java.util.Scanner; import com.github.scribejava.core.builder.ServiceB… Read More
  • Yahoo OAuth 1.0a Example using Scribe JavaIn this example we are going to see about how to use Yahoo OAuth 1.0a using Scribe Java Yahoo OAuth 1.0a Example with Scope YahooExample.java import java.util.Scanner; import com.github.scribejava.core.builder.ServiceBuilder… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Salesforce OAuth 2.0 Example using Scribe Java Rating: 5 Reviewed By: eHowToNow