Salesforce REST API 登录?

17
我正在检查Salesforce开发者网站上的一个示例。
在该示例中,当我们点击链接时,将会被重定向到Salesforce登录页面。如果登录成功,则会发出访问令牌。
我不想让我的应用程序重定向到Salesforce登录页面。在现有示例中,环境变量设置为:

"https://login.salesforce.com"

为了避免重定向到Salesforce登录页面,你需要更改环境变量的值并将其设置为Salesforce OAuth授权端点。OAuth授权端点将返回访问令牌而无需进行任何重定向。
4个回答

13

感谢您的回复。我发现我需要使用用户名密码 OAuth 流程来满足我的需求。https://login.salesforce.com/help/doc/en/remoteaccess_oauth_username_password_flow.htm => 这是我们需要参考的链接。但尽管我已经按照步骤操作,我的应用程序仍然重定向失败。:( - Easwaramoorthy K
1
你是否正确地遵循了“OAuth舞蹈”的步骤?请记住,你需要3个东西:请求令牌URL、API URL和访问令牌URL。 - David
1
解决方案:大家好,我已经找到了解决我的问题的方法。实际上,我正在检查链接http://wiki.developerforce.com/page/Getting_Started_with_the_Force.com_REST_API中提供的示例。然后实施了来自https://login.salesforce.com/help/doc/en/remoteaccess_oauth_username_password_flow.htm#send_up_response的OAuth 2.0用户名密码流。它解决了我的问题。感谢您的回复。 - Easwaramoorthy K
只是让您知道,用户名密码流仅建议用于测试目的,如果您需要一个非使用交互的JWT Bearer令牌,则建议使用。 - Gareth Jordan
又因链接失效而丢失了另一个答案。 - Gusk

6

5

这是一个使用用户名密码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();
        }
    }
}

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接