본문 바로가기

안드로이드

안드로이드 네이버 로그인

1) 네이버 개발자 , 네이버 로그인 api 로 가서  OAUTH_CLIENT_ID  , OAUTH_CLIENT_SECRET  를 받아옵니다.

2) naver-oauth-login-4.1.4.1-with-sources.jar 파일을 libs 폴더에 넣어줍니다. 

3) activity_login 레이아웃을 하나 만들고 btn_naver 아이디로 버튼을 하나 만들어 줍니다. 


4)소스 

=====================================================================================



public class LogInActivity extends Activity {


private static String OAUTH_CLIENT_ID = "80nOCZ8Up0xxxxxxxxxxxx";  // 1)에서 받아온 값들을 넣어좁니다

private static String OAUTH_CLIENT_SECRET = "Lo7vxxxxxxxxxx";

private static String OAUTH_CLIENT_NAME = "네이버 아이디로 로그인";


private static OAuthLogin mOAuthLoginInstance;

private static Context mContext;


String email = "";

String nickname = "";

String enc_id = "";

String profile_image = "";

String age = "";

String gender = "";

String id = "";

String name = "";

String birthday = "";


String accessToken = "";

String tokenType;




@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);


mContext = getApplicationContext();


setContentView(R.layout.activity_login);


mOAuthLoginInstance = OAuthLogin.getInstance();


mOAuthLoginInstance.init(mContext, OAUTH_CLIENT_ID,

OAUTH_CLIENT_SECRET, OAUTH_CLIENT_NAME);

initSetting();

}


private void initSetting() {


final Button btn_naver = (Button) findViewById(R.id.btn_naver);



btn_naver.setOnClickListener(new View.OnClickListener() {


@Override

public void onClick(View v) {

mOAuthLoginInstance.startOauthLoginActivity(LogInActivity.this,

mOAuthLoginHandler);

}

});


}


private OAuthLoginHandler mOAuthLoginHandler = new OAuthLoginHandler() {

@Override

public void run(boolean success) {

if (success) {

accessToken = mOAuthLoginInstance.getAccessToken(mContext);

String refreshToken = mOAuthLoginInstance

.getRefreshToken(mContext);

long expiresAt = mOAuthLoginInstance.getExpiresAt(mContext);

tokenType = mOAuthLoginInstance.getTokenType(mContext);


Log.d("myLog", "accessToken  " + accessToken);

Log.d("myLog", "refreshToken  " + refreshToken);

Log.d("myLog",

"String.valueOf(expiresAt)  "

+ String.valueOf(expiresAt));

Log.d("myLog", "tokenType  " + tokenType);

Log.d("myLog",

"mOAuthLoginInstance.getState(mContext).toString()  "

+ mOAuthLoginInstance.getState(mContext)

.toString());



new RequestApiTask().execute(); //로그인이 성공하면  네이버에 계정값들을 가져온다.


} else {

String errorCode = mOAuthLoginInstance.getLastErrorCode(

mContext).getCode();

String errorDesc = mOAuthLoginInstance

.getLastErrorDesc(mContext);

// Toast.makeText(mContext, "errorCode:" + errorCode +

// ", errorDesc:" + errorDesc, Toast.LENGTH_SHORT).show();


Toast.makeText(LogInActivity.this, "로그인이 취소/실패 하였습니다.!",

Toast.LENGTH_SHORT).show();

}

};

};


private class RequestApiTask extends AsyncTask<Void, Void, Void> {

@Override

protected void onPreExecute() {


}


@Override

protected Void doInBackground(Void... params) {

String url = "https://openapi.naver.com/v1/nid/getUserProfile.xml";

String at = mOAuthLoginInstance.getAccessToken(mContext);

Pasingversiondata(mOAuthLoginInstance.requestApi(mContext, at, url));


return null;

}


protected void onPostExecute(Void content) {

Log.d("myLog", "email " + email);

Log.d("myLog", "name " + name);

Log.d("myLog", "nickname " + nickname);


if (email == null) {

Toast.makeText(LogInActivity.this,

"로그인 실패하였습니다.  잠시후 다시 시도해 주세요!!",                 Toast.LENGTH_SHORT)

.show();

} else {


}


}


private void Pasingversiondata(String data) { // xml 파싱

String f_array[] = new String[9];


try {

XmlPullParserFactory parserCreator = XmlPullParserFactory

.newInstance();

XmlPullParser parser = parserCreator.newPullParser();

InputStream input = new ByteArrayInputStream(

data.getBytes("UTF-8"));

parser.setInput(input, "UTF-8");


int parserEvent = parser.getEventType();

String tag;

boolean inText = false;

boolean lastMatTag = false;


int colIdx = 0;


while (parserEvent != XmlPullParser.END_DOCUMENT) {

switch (parserEvent) {

case XmlPullParser.START_TAG:

tag = parser.getName();

if (tag.compareTo("xml") == 0) {

inText = false;

} else if (tag.compareTo("data") == 0) {

inText = false;

} else if (tag.compareTo("result") == 0) {

inText = false;

} else if (tag.compareTo("resultcode") == 0) {

inText = false;

} else if (tag.compareTo("message") == 0) {

inText = false;

} else if (tag.compareTo("response") == 0) {

inText = false;

} else {

inText = true;


}

break;

case XmlPullParser.TEXT:

tag = parser.getName();

if (inText) {

if (parser.getText() == null) {

f_array[colIdx] = "";

} else {

f_array[colIdx] = parser.getText().trim();

}


colIdx++;

}

inText = false;

break;

case XmlPullParser.END_TAG:

tag = parser.getName();

inText = false;

break;


}


parserEvent = parser.next();

}

} catch (Exception e) {

Log.e("dd", "Error in network call", e);

}

email = f_array[0];

nickname = f_array[1];

enc_id = f_array[2];

profile_image = f_array[3];

age = f_array[4];

gender = f_array[5];

id = f_array[6];

name = f_array[7];

birthday = f_array[8];


}

}

}