1. cURL Commands
  2. Postman App
  3. Python Code
  4. PHP Code
  5. Java Code

cURL Commands

To generate access token using cURL command, the following steps needs to be followed

  • Download cURL for your respective environment.
  • Execute the following command wherein : stands for your Client ID and Secret Key respectively.
    • curl -v https://api-uat.statestreet.com/oauth/accesstoken \ 
           -H "Accept: application/json" \     
           -u " client_id : secret " \     
           -d "grant_type=client_credentials"
      		 
  • If executed successfully, you'd get the following result :
    • {                                                                      
      "token_type"       : "BearerToken",     
      "access_token"     : "Token will be provided here in alphanumeric format", 
      “application_name" : "sample-application",       
      "expires_in"       : "3600",     
      "refresh_count"    : "0",      
      "status"           : "approved"  
      }  
  • The Access Token comes with an expiration date. The expires_in field in the above result conveys the expiration time to the consumer in number of seconds. The consumer can reuse the Access Token till it's expiry. Once the times lapses, the consumer can apply for a new token.

 

Postman App

  • Download Postman App for your respective environment.
  • Once opened, create a new Request with the POST method.
  • Within the Request URL Section, enter the following URL https://api-uat.statestreet.com/oauth/accesstoken.
  • Navigate to the Authorization Tab and select the Basic Auth type. Type your Client ID and Secret Key in the Username and Password fields respectively.
  • Select x-www-form-urlencoded in the Body tab. Enter grant_type in the Key field and client_credentials in the Value field.
  • On performing the Send operation, one can obtain the access token from the access_token field from the response sent by State Street Authorization Server. For example :
    • {
      "token_type"       : "BearerToken",      
      "access_token"     : "Token will be provided here in alphanumeric format",
      “application_name" : "sample-application",        
      "expires_in"       : "3600",      
      "refresh_count"    : "0",      
      "status"           : "approved"  
      }  
  • The Access Token comes with an expiration date. The expires_in field in the above result conveys the Expiration Time to the consumer in number of seconds. The consumer can reuse the Access Token till it's expiry. Once the times lapses, the consumer can apply for a new token.

Python Code

  • Download Python for your environment.
  • Include the following code in your script.
    • def getAuthToken2(username,password,url):
          base64string = base64.b64encode('%s:%s' % (username, password))
          data = urllib.urlencode({'grant_type' : 'client_credentials'})
          request = urllib2.Request(url,data)
          request.add_header('Authorization', 'Basic '+base64string)
          request.add_header('Accept',"application/json")
          request.add_header('Content-type', "application/x-www-form-urlencoded")
          request.get_method = lambda: 'POST'
      	response = json.loads(urllib2.urlopen(request).read())
      	return (response['access_token'])
      		
  • Populate a Request with the POST method.Type your Client ID and Secret Key in the Username and Password fields respectively.
  • Within the URL Section, pass following URL https://api-uat.statestreet.com/oauth/accesstoken .
  • Make sure you pass x-www-form-urlencoded as Content-type. Also, pass grant_type as client_credentials.

PHP Code

  • Download PHP for your environment.
  • Include the following function in your script.
    • function getAuthToken($url,$client_app_key,$client_app_secret){
        $data = array('grant_type' =  'client_credentials');
        $options = array(
         'http' = array(
      	'header' =  array ('Content-type: application/x-www-form-urlencoded',
      		'Accept: application/json', 
                	'Authorization: Basic '.base64_encode("$client_app_key:$client_app_secret"))
      	 	'method' =  'POST',
      		'content' =  http_build_query($data)
      	)
      		);
      	$context  = stream_context_create($options);
      	$result = file_get_contents($url , false, $context);
      	if ($result === FALSE) { /* Handle error */ }
      	$data=json_decode($result, true);
      	return $data['access_token'];
      }
      
      $url = 'https://api-uat.statestreet.com';
      
      $authToken= getAuthToken($url.'/oauth/accesstoken',$client_app_key,$client_app_secret);
      

  • Populate a Request with the POST method. Pass your Client ID and Secret Key in the $client_app_key and $client_app_secret fields respectively.
  • Pass following URL https://api-uat.statestreet.com for $url.

Java Code

  • Download Java for your environment.
  • Include the following method in your script.
    • 
      private static String getAUthToken(String URL,String client_app_key,String client_app_secret) throws IOException {
      URL obj = new URL(URL);
      String auth = client_app_key + ":" + client_app_secret;
      byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(StandardCharsets.UTF_8));
      String authHeaderValue = "Basic " + new String(encodedAuth);
      HttpURLConnection con = (HttpURLConnection) obj.openConnection();
      con.setRequestMethod("POST");
      con.setRequestProperty("Authorization", authHeaderValue);
      con.setRequestProperty("Accept", "application/json");
      con.setRequestProperty("User-Agent", USER_AGENT);
      final String POST_PARAMS = "grant_type=client_credentials";
      // For POST only - START
      con.setDoOutput(true);
      OutputStream os = con.getOutputStream();
      os.write(POST_PARAMS.getBytes());
      os.flush();
      os.close();
      // For POST only - END
      int responseCode = con.getResponseCode();
      System.out.println("POST Response Code :: " + responseCode);
      if (responseCode == HttpURLConnection.HTTP_OK) { //success
      		BufferedReader in = new BufferedReader(new InputStreamReader(
      		     con.getInputStream()));
      		String inputLine;
      		StringBuffer response = new StringBuffer();
      		while ((inputLine = in.readLine()) != null) {
      		  response.append(inputLine);
      		}
      		in.close();
      		Pattern p = Pattern.compile("\"access_token\":\"(\\w)*\",");
      		Matcher m = p.matcher(response.toString());
      		if (m.find()) {
      			String group = m.group();
      			return group.substring(16,group.length()-2);
      		}
      		else
      		{
      			throw new IOException("Access Token Not found");
      		}
      } else {
      		System.out.println("POST request not worked");
      		throw new IOException("POST request not worked");
      }
      }
      
      
  • Populate a Request with the POST method. Pass your Client ID and Secret Key in the client_app_key and client_app_secret fields respectively.
  • Pass following URL https://api-uat.statestreet.com for URL.