com.ny.apps.executor.TencentWeiboOAuth2.java Source code

Java tutorial

Introduction

Here is the source code for com.ny.apps.executor.TencentWeiboOAuth2.java

Source

/**
 * 
 * @Title TencentWeiboOAuth2.java
 * @Description TODO
 * Copyright: Copyright (c) 2013, Smiletony and/or its affiliates. All rights reserved.
 * SMILETONY PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @author NY
 * @date 2013-10-17 ?10:43:38
 * 
 */
package com.ny.apps.executor;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @Description TODO
 * @author <a href="mailto:mynameisny@qq.com">Tony Joseph</a>
 * @version TODO
 *
 */
public class TencentWeiboOAuth2 {
    public String requestURL = "https://open.t.qq.com/cgi-bin/oauth2/authorize";
    public static final String APPKEY = "";
    public static final String APPSECRET = "";

    private Logger logger = LoggerFactory.getLogger(TencentWeiboOAuth2.class);

    public static void main(String[] args) {
        try {
            System.out.println(new TencentWeiboOAuth2().getCode(APPKEY, APPSECRET));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getCode(String APPKEY, String APPSECRET) throws Exception {
        String code = null;

        CloseableHttpClient client = HttpClients.createDefault();

        HttpGet httpGet = new HttpGet(prepareGetUrl());

        System.out.println(prepareGetUrl().toASCIIString());

        logger.info("executing request " + httpGet.getURI());

        ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
            @Override
            public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode >= 200 && statusCode < 300) {
                    HttpEntity entity = response.getEntity();
                    return entity == null ? null : EntityUtils.toString(entity);
                } else {
                    throw new ClientProtocolException("Unexpected response status: " + statusCode);
                }
            }
        };

        try {
            String responseBody = client.execute(httpGet, responseHandler);
            System.out.println("----------------------------------------");
            System.out.println(responseBody);
            System.out.println("----------------------------------------");
        } finally {
            client.close();
        }

        return code;
    }

    private URI prepareGetUrl() {
        URI uri = null;

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        NameValuePair clientId = new BasicNameValuePair("client_id", APPKEY);
        NameValuePair responseType = new BasicNameValuePair("response_type", "code");
        NameValuePair redirectURI = new BasicNameValuePair("redirect_uri", "http://www.smiletony.com");
        nvps.add(clientId);
        nvps.add(responseType);
        nvps.add(redirectURI);

        URIBuilder uriBuilder;
        try {
            uriBuilder = new URIBuilder("https://open.t.qq.com/cgi-bin/oauth2/authorize");
            uriBuilder.addParameters(nvps);
            uri = uriBuilder.build();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }

        return uri;
    }
}