Skip to content

Instantly share code, notes, and snippets.

@artronics
Created April 8, 2021 13:29
Show Gist options
  • Save artronics/6558564a3b4423d9783f54d4aedc83fa to your computer and use it in GitHub Desktop.
Save artronics/6558564a3b4423d9783f54d4aedc83fa to your computer and use it in GitHub Desktop.
package com.example.hellodemo;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class Application {
public static void main(String[] args) throws IOException {
OAuth oAuth = new OAuth();
oAuth.getCode();
oAuth.getAccessToken();
}
}
class OAuth {
final private String authorizationUri = "https://sandbox.api.service.nhs.uk/oauth2/authorize"; // Authorization code endpoint
final private String redirectUri = "http://localhost:5000/callback"; // Callback endpoint
final private String tokenUri = "https://sandbox.api.service.nhs.uk/oauth2/token"; // Get tokens endpoint
final private String clientId= "your-client-id"; // change to your Client_ID
final private String clientSecret = "your-client-secret"; // change to your client_secret
final private String state = "random";
public void getCode() {
System.out.println("Open the link below in your browser");
System.out.println(authorizationUri +
"?client_id=" + clientId +
"&redirect_uri=" + redirectUri +
"&response_type=code" +
"&state=" + state);
}
public void getAccessToken() throws IOException {
Scanner in = new Scanner(System.in);
System.out.println("Enter code:");
String code = in.nextLine();
HttpPost httpPost = new HttpPost(tokenUri);
List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("grant_type", "authorization_code"));
params.add(new BasicNameValuePair("client_id", clientId));
params.add(new BasicNameValuePair("client_secret", clientSecret));
params.add(new BasicNameValuePair("redirect_uri", redirectUri));
params.add(new BasicNameValuePair("code", code));
httpPost.setEntity(new UrlEncodedFormEntity(params));
CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
String text = new BufferedReader(
new InputStreamReader(instream, StandardCharsets.UTF_8)).lines()
.collect(Collectors.joining("\n"));
System.out.println(text);
instream.close();
}
client.close();
}
}
apply plugin: 'java'
apply plugin: 'application'
mainClassName = 'com.example.hellodemo.Application'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
mavenCentral()
}
dependencies {
compile 'org.apache.httpcomponents:httpclient:4.5.13'
}
jar {
manifest {
attributes('Main-Class': mainClassName)
}
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment