Chilkat Online Tools

Android / Salesforce Platform APIs / Username Password Login - Authorize

Back to Collection Items

// Important: Don't forget to include the call to System.loadLibrary
// as shown at the bottom of this code sample.
package com.test;

import android.app.Activity;
import com.chilkatsoft.*;

import android.widget.TextView;
import android.os.Bundle;

public class SimpleActivity extends Activity {

  private static final String TAG = "Chilkat";

  // Called when the activity is first created.
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    CkHttp http = new CkHttp();
    boolean success;

    CkHttpRequest req = new CkHttpRequest();
    req.AddParam("response_type","code_credentials");

    CkJsonObject jsonParam2 = new CkJsonObject();
    req.AddParam("client_id",jsonParam2.emit());

    CkJsonObject jsonParam3 = new CkJsonObject();
    req.AddParam("redirect_uri",jsonParam3.emit());

    req.AddHeader("Auth-Request-Type","Named-User");
    req.AddHeader("Authorization","Basic <username:password>");

    CkHttpResponse resp = http.PostUrlEncoded("https://login.salesforce.com{{site}}/services/oauth2/authorize",req);
    if (http.get_LastMethodSuccess() == false) {
        Log.i(TAG, http.lastErrorText());
        return;
        }

    Log.i(TAG, String.valueOf(resp.get_StatusCode()));
    Log.i(TAG, resp.bodyStr());


  }

  static {
      System.loadLibrary("chilkat");

      // Note: If the incorrect library name is passed to System.loadLibrary,
      // then you will see the following error message at application startup:
      //"The application <your-application-name> has stopped unexpectedly. Please try again."
  }
}

Curl Command

curl -X POST
	-H "Auth-Request-Type: Named-User"
	-H "Authorization: Basic <username:password>"
	--data-urlencode 'response_type=code_credentials'
	--data-urlencode 'client_id={{clientId}}'
	--data-urlencode 'redirect_uri={{redirectUrl}}'
https://login.salesforce.com{{site}}/services/oauth2/authorize

Postman Collection Item JSON

{
  "name": "Username Password Login - Authorize",
  "request": {
    "auth": {
      "type": "noauth"
    },
    "method": "POST",
    "header": [
      {
        "key": "Auth-Request-Type",
        "value": "Named-User",
        "description": "Required for Username Password Login"
      },
      {
        "key": "Authorization",
        "value": "Basic <username:password>",
        "description": "Base 64 encoded username:password"
      }
    ],
    "body": {
      "mode": "urlencoded",
      "urlencoded": [
        {
          "key": "response_type",
          "value": "code_credentials",
          "type": "text"
        },
        {
          "key": "client_id",
          "value": "{{clientId}}",
          "type": "text"
        },
        {
          "key": "redirect_uri",
          "value": "{{redirectUrl}}",
          "type": "text"
        },
        {
          "key": "scope",
          "value": "",
          "description": "Comma seperated list of scopes, optional",
          "type": "text",
          "disabled": true
        }
      ]
    },
    "url": {
      "raw": "{{url}}{{site}}/services/oauth2/authorize",
      "host": [
        "{{url}}{{site}}"
      ],
      "path": [
        "services",
        "oauth2",
        "authorize"
      ]
    },
    "description": "This request allows named users to use their username and password to get an access token and refresh token from Salesforce in a headless manner. There are two headers that must be included, the first is the `Auth-Request-Type` which must be set to `Named-User`, and then there is the `Authorization` Basic header which contains the base 64 encoded username:password of the user.\n\nThe result of this API call, is an Auth Code, which is then exchanged with another call at the token endpoint."
  },
  "response": [
  ]
}