Java oauh2 google get access token

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.JSONValue;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

private String getAccessToken(String code, String clientId, String clientSecret, String redirectUri,
            HttpServletResponse hsr)
            throws SSOException {
        String accessToken = "";
        try {

         
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost postRequest = new HttpPost(
                    "https://www.googleapis.com/oauth2/v4/token");
           
            List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
            urlParameters.add(new BasicNameValuePair("grant_type", "authorization_code"));
            urlParameters.add(new BasicNameValuePair("client_id", clientId));
            urlParameters.add(new BasicNameValuePair("client_secret", clientSecret));
            urlParameters.add(new BasicNameValuePair("code", code));
            urlParameters.add(new BasicNameValuePair("redirect_uri", redirectUri));
           
            postRequest.setEntity(new UrlEncodedFormEntity(urlParameters));
           
            HttpResponse response = httpClient.execute(postRequest);

            if (response.getStatusLine().getStatusCode() != 200) {
                              throw new Exception("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuilder sb = new StringBuilder();
            String output;

            while ((output = br.readLine()) != null) {
                sb.append(output);
            }
            br.close();
          
            JSONObject jsonObject = new JSONObject(sb.toString());
            accessToken = jsonObject.getString("access_token");

            httpClient.getConnectionManager().shutdown();

        } catch (IOException | JSONException e) {
           
        }
        return accessToken;
    }

Java twitter post status

import java.net.URLEncoder;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import oauth.signpost.OAuthConsumer;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;

public class JavaRestTweet {
    private static final String CONSUMER_KEY_STR = "";
    private static final String CONSUMER_SECRET_STR = "";
    private static final String ACCESSTOKEN_STR = "";
    private static final String ACCESSTOKEN_SECRET_STR = "";
    private static final String API_URL = "https://api.twitter.com/1.1/statuses/update.json?status=";

    public static void main(String[] args) throws Exception {
        postTweet("Hi Twitter - test");
    }

    public static void postTweet(String message) throws Exception {
        OAuthConsumer oAuthConsumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY_STR, CONSUMER_SECRET_STR);
        oAuthConsumer.setTokenWithSecret(ACCESSTOKEN_STR, ACCESSTOKEN_SECRET_STR);

        HttpPost httpPost = new HttpPost(API_URL + URLEncoder.encode(message, "UTF-8"));
        oAuthConsumer.sign(httpPost);

        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = httpClient.execute(httpPost);

        int statusCode = httpResponse.getStatusLine().getStatusCode();
        System.out.println(statusCode + ':' + httpResponse.getStatusLine().getReasonPhrase());
        System.out.println(IOUtils.toString(httpResponse.getEntity().getContent()));

    }
}

Java facebook oauth2 get access token

 private static final String ACCESS_TOKEN_URL = "https://graph.facebook.com/oauth/access_token?client_id=%s&redirect_uri=%s&client_secret=%s&code=%s";

private String getAccessToken(String code, String clientId, String clientSecret,  
                String redirectUri,
                HttpServletResponse hsr) throws Exception {
        String accessToken = "";
        try {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet getRequest = new HttpGet(String.format(ACCESS_TOKEN_URL, clientId,
                    URLEncoder.encode(redirectUri, "UTF-8"), clientSecret, code));
            getRequest.addHeader("oauth2_access_token", code);
            HttpResponse response = httpClient.execute(getRequest);

            if (response.getStatusLine().getStatusCode() != 200) {
                          throw new Exception("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuilder sb = new StringBuilder();
            String output;

            while ((output = br.readLine()) != null) {
                sb.append(output);
            }
            br.close();
           
            accessToken = sb.toString();

            httpClient.getConnectionManager().shutdown();

        } catch (IOException e) {
                    }
        return accessToken;
    }

Java LinkedIn OAuth2 get access token

Pass the appropriate arguments and get authetication token.

private String getLinkedInAccessToken(String authorizationCode, String clientId, String clientSecret, String redirectUri, HttpServletResponse hsr) throws Exception {
        String accessToken = "";
        try {
         
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet getRequest = new HttpGet(
                    "https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&client_id="
                            + clientId + "&client_secret=" + clientSecret + "&code=" + authorizationCode + "&redirect_uri="
                            + redirectUri);
            getRequest.addHeader("oauth2_access_token", code);
            HttpResponse response = httpClient.execute(getRequest);

            if (response.getStatusLine().getStatusCode() != 200) {
                ExceptionWriter.showMsg(hsr, "Error in get access token\n" + "Request Status Code: "+
                        response.getStatusLine().getStatusCode());
                throw new Exception("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
            }
            BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuilder sb = new StringBuilder();
            String output;

            while ((output = br.readLine()) != null) {
                sb.append(output);
            }
            br.close();
            LOGGER.info("JSON Object" + sb);

            JSONObject jsonObject = new JSONObject(sb.toString());
            accessToken = jsonObject.getString("access_token");

            httpClient.getConnectionManager().shutdown();

        } catch (IOException | JSONException e) {
        }
        return accessToken;
    }

Include required jars in your project to run this method.

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.simple.JSONValue;
-----
-----
etc

Java get MAC address of Computer

    /**
     * This method get the MAC address of a computer
     *
     * @return string of MAC address of computer
     */
    public static String getMacAddres() {
        String macAddress = "";
        try {
            for (Enumeration<NetworkInterface> enm = NetworkInterface.getNetworkInterfaces(); enm.hasMoreElements();) {
                NetworkInterface network1 = (NetworkInterface) enm.nextElement();
                if (null != network1.getHardwareAddress()) {
                    byte[] mac = new byte[50];

                    mac = network1.getHardwareAddress();

                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < mac.length; i++) {
                        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                    }

                    macAddress = sb.toString();
                }
            }
        } catch (SocketException e) {
            // e.printStackTrace();
        }

        return macAddress;
    }