简体   繁体   English

Salesforce REST API 登录?

[英]Salesforce REST API Login?

I am examining a sample which is in the salesforce developer site.我正在检查 salesforce 开发人员站点中的一个示例。

In that sample when we click a link it will be redirected to the salesforce login page.在该示例中,当我们单击链接时,它将被重定向到 Salesforce 登录页面。 If the login successful, then an access token is issued.如果登录成功,则会发出访问令牌。

I dont want my application to redirect to the salesforce login page.我不希望我的应用程序重定向到 Salesforce 登录页面。 In the existing sample the environment variable is set to,在现有示例中,环境变量设置为,

"https://login.salesforce.com" “https://login.salesforce.com”

What should I do to avoid redirecting to salesforce login page.我应该怎么做才能避免重定向到 Salesforce 登录页面。

What you're describing, sounds like OAuth (just because you mention access-token).您所描述的内容听起来像 OAuth(只是因为您提到了访问令牌)。

There's a good example of OAuth being used in Salesforce below...下面是在 Salesforce 中使用 OAuth 的一个很好的例子......

http://wiki.developerforce.com/page/Digging_Deeper_into_OAuth_2.0_at_Salesforce.com http://wiki.developerforce.com/page/Digging_Deeper_into_OAuth_2.0_at_Salesforce.com

SOLUTION:解决方案:

Hi all, I have arrived the solution to my problem.大家好,我已经找到了解决我的问题的方法。 Actually, I was examining the sample given in the link http://wiki.developerforce.com/page/Getting_Started_with_the_Force.com_REST_API .实际上,我正在检查链接http://wiki.developerforce.com/page/Getting_Started_with_the_Force.com_REST_API 中给出的示例。 Then implemented OAuth 2.0 Username-Password Flow which is from https://login.salesforce.com/help/doc/en/remoteaccess_oauth_username_password_flow.htm#send_up_response .然后实现了来自https://login.salesforce.com/help/doc/en/remoteaccess_oauth_username_password_flow.htm#send_up_response 的OAuth 2.0 用户名-密码流。 It solves my problem.它解决了我的问题。

This is sample Java code that uses Username-Password OAuth flow:这是使用用户名-密码 OAuth 流程的示例 Java 代码:

public class AccountQuery 
{
    // The connection data
    private static final String query = "SELECT Name, Idfrom Account";
    private static final String clientId = "theID";
    private static final String clientSecret = "theSecret";
    // THis is meaningless in our context
    private static final String redirectUri = "https://localhost:8443/_callback";
    private static final String environment = "https://login.salesforce.com";   
    private static String tokenUrl = null;
    private static final String username = "username";
    private static final String password = "passwordPlusSecret";
    private static String accessToken = null;
    private static String instanceUrl = null;

    public static void main( String[] args )
    {       
        // Step 0:  Connect to SalesForce.
        System.out.println("Getting a token");
        tokenUrl = environment + "/services/oauth2/token";
        HttpClient httpclient = new HttpClient();
        PostMethod post = new PostMethod(tokenUrl);     
        post.addParameter("grant_type", "password");
        post.addParameter("client_id", clientId);
        post.addParameter("client_secret", clientSecret);
        post.addParameter("redirect_uri", redirectUri);
        post.addParameter("username", username);
        post.addParameter("password", password);

        try {
            httpclient.executeMethod(post);
            try {
                JSONObject authResponse = new JSONObject(new JSONTokener(new InputStreamReader(post.getResponseBodyAsStream())));
                System.out.println("Auth response: " + authResponse.toString(2));

                accessToken = authResponse.getString("access_token");
                instanceUrl = authResponse.getString("instance_url");

                System.out.println("Got access token: " + accessToken);
            } catch (JSONException e) {
                e.printStackTrace();                
            }
        } catch (HttpException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } finally {
            post.releaseConnection();
        }       
        System.out.println("We have an access token: " + accessToken + "\n" + "Using instance " + instanceUrl + "\n\n");

        HttpClient httpclient = new HttpClient();
        GetMethod get = new GetMethod(instanceUrl + "/services/data/v28.0/query");

        // set the token in the header
        get.setRequestHeader("Authorization", "OAuth " + accessToken);

        // set the SOQL as a query param
        NameValuePair[] params = new NameValuePair[1];

        params[0] = new NameValuePair("q",query);
        get.setQueryString(params);     

        try {
            httpclient.executeMethod(get);
            if (get.getStatusCode() == HttpStatus.SC_OK) {
                // Now lets use the standard java json classes to work with the results
                JSONObject response = new JSONObject( new JSONTokener( new InputStreamReader(get.getResponseBodyAsStream())));
                System.out.println("Query response: "+ response.toString(2));//.substring(0, 500));                 
                System.out.println(response.getString("totalSize") + " record(s) returned\n\n");
                JSONArray results = response.getJSONArray("records");               
                Account[] accounts = new Gson().fromJson(results.toString(), Account[].class);
                return accounts;
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }finally {
            get.releaseConnection();
        }
    }
}

In case of Java, the easiest way is to use Spring Social Salesforce: https://github.com/iirekm/spring-social-salesforce对于 Java,最简单的方法是使用 Spring Social Salesforce: https : //github.com/iirekm/spring-social-salesforce

Just follow the Facebook tutorial at https://spring.io/guides/gs/accessing-facebook/ , and shortly-speaking add dependency to Spring Social Salesforce and replace everywhere in code "facebook" with "salesforce".只需按照https://spring.io/guides/gs/accessing-facebook/ 上的 Facebook 教程进行操作,简而言之,将依赖项添加到 Spring Social Salesforce,并将代码“facebook”中的所有位置替换为“salesforce”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM