본문 바로가기

닷컴's_열공/JAVA

oauth 샘플

 

import java.io.BufferedReader;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.Scanner;

 

import oauth.signpost.OAuth;

import oauth.signpost.OAuthConsumer;

import oauth.signpost.OAuthProvider;

import oauth.signpost.basic.DefaultOAuthConsumer;

import oauth.signpost.basic.DefaultOAuthProvider;

import oauth.signpost.signature.SignatureMethod;

 

public class Main {

 

private static final String REQUEST_TOKEN_URL = "https://nid.naver.com/naver.oauth?mode=req_req_token";

private static final String AUTHORIZE_URL = "https://nid.naver.com/naver.oauth?mode=auth_req_token";

private static final String ACCESS_TOKEN_URL = "https://nid.naver.com/naver.oauth?mode=req_acc_token";

private static final String CONSUMER_KEY = "your key";

private static final String CONSUMER_SECRET = "your key...";

private static final int cafeid = 1111111111;

 

    public static void main(String[] args) throws Exception {

 

        OAuthConsumer consumer = new DefaultOAuthConsumer(

        CONSUMER_KEY,

        CONSUMER_SECRET,

                SignatureMethod.HMAC_SHA1);

 

        OAuthProvider provider = new DefaultOAuthProvider(consumer,

        REQUEST_TOKEN_URL,

        ACCESS_TOKEN_URL,

        AUTHORIZE_URL);

 

        System.out.println("Fetching request token from naver...");

 

        // we do not support callbacks, thus pass OOB

        String authUrl = provider.retrieveRequestToken(OAuth.OUT_OF_BAND);

 

        System.out.println("Request token: " + consumer.getToken());

        System.out.println("Token secret: " + consumer.getTokenSecret());

 

        System.out.println("Now visit (copy and past web brower):\n" + authUrl

                + "\n... and watch brower address find aouth_verifier. ");

        System.out.println("sample : https://nid.naver.com/oob?oauth_token=SbiibfFhUZRrxqe1&oauth_verifier=qweer1234");

        System.out.println("Enter the oauth_verifier code  and hit ENTER when you're done:");

 

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String pin = br.readLine();

 

        System.out.println("Fetching access token from Twitter...");

 

        provider.retrieveAccessToken(pin);

 

        System.out.println("Access token: " + consumer.getToken());

        System.out.println("Token secret: " + consumer.getTokenSecret());

 

        URL url = new URL("http://twitter.com/statuses/mentions.xml");

        HttpURLConnection request = (HttpURLConnection) url.openConnection();

 

        consumer.sign(request);

 

        System.out.println("Sending request to Twitter...");

        request.connect();

 

        System.out.println("Response: " + request.getResponseCode() + " "

                + request.getResponseMessage());

        System.out.println("cafe text");

        

        System.out.println(getCafeRecentPosts(consumer, cafeid, 1));

        

    }

    public static String getCafeRecentPosts(OAuthConsumer consumer, int clubId, int perPage)

{

URL url;

StringBuffer buffer = new StringBuffer();

Scanner scann = null;

try {

url = new URL("http://openapi.naver.com/cafe/getArticleList.xml?search.clubid="+clubId+"&search.perPage="+perPage);

HttpURLConnection request = (HttpURLConnection) url.openConnection();

consumer.sign(request);

request.connect();

InputStream is = request.getInputStream();

scann = new Scanner(is, "UTF-8");

while(scann.hasNextLine())

{

buffer.append(scann.nextLine());

}

NaverCafeNewPost (2).zip
다운로드

 

}catch (Exception e) {

e.printStackTrace();

}finally{

if ( scann != null ) scann.close();

}

return buffer.toString();

}

}

 

 

'닷컴's_열공 > JAVA' 카테고리의 다른 글

[퍼옴]Excel파일 Read/Write  (0) 2019.10.31
equals()의 바른 사용법  (0) 2019.02.20
OAUTH 관련  (0) 2016.07.06
org.apache.commons.io 사용  (0) 2012.02.28
java split 잘안될경우  (2) 2011.06.17