[Android Studio] OAuth 1. 네이버 로그인

Runtime Error·2021년 4월 20일
0

Android Studio

목록 보기
3/4
post-custom-banner

AndroidManifest.xml

<activity android:name="com.nhn.android.naverlogin.ui.OAuthLoginActivity" />
<activity android:name="com.nhn.android.naverlogin.ui.OAuthLoginInAppBrowserActivity"
            android:label="OAuth2.0 In-app"/>

build.gradle(app)

implementation 'com.naver.nid:naveridlogin-android-sdk:4.2.6'

activity_main.xml

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <ImageButton
                android:id="@+id/naverButton"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_weight="1"
                app:srcCompat="@drawable/login_naver" />
            <Button
                android:id="@+id/buttonLogout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:text="LOGOUT" />
        </LinearLayout>

MainActivity.java

context = getApplicationContext();
        naverButton = findViewById(R.id.naverButton);
        buttonLogout = findViewById(R.id.buttonLogout);
        naverButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                oAuthLogin = OAuthLogin.getInstance();
                oAuthLogin.init(
                        context
                        ,getString(R.string.naver_client_id)
                        ,getString(R.string.naver_client_secret)
                        ,getString(R.string.naver_client_name)
                );
                @SuppressLint("HandlerLeak")
                OAuthLoginHandler oAuthLoginHandler = new OAuthLoginHandler() {
                    @Override
                    public void run(boolean success) {
                        if (success) {
                            String accessToken = oAuthLogin.getAccessToken(context);
                            String refreshToken = oAuthLogin.getRefreshToken(context);
                            long expiresAt = oAuthLogin.getExpiresAt(context);
                            String tokenType = oAuthLogin.getTokenType(context);
                            Log.i("LoginData", "accessToken: " + accessToken);
                            Log.i("LoginData", "refreshToken: " + refreshToken);
                            Log.i("LoginData", "expiresAt: " + expiresAt);
                            Log.i("LoginData", "tokenType: " + tokenType);
                        } else {
                            String errorCode = oAuthLogin
                                    .getLastErrorCode(context).getCode();
                            String errorDesc = oAuthLogin.getLastErrorDesc(context);
                            Toast.makeText(context, "errorCode : " + errorCode + ", erorDesc: " + errorDesc, Toast.LENGTH_SHORT).show();
                        }
                    };
                };
                oAuthLogin.startOauthLoginActivity(MainActivity.this, oAuthLoginHandler);
            }
        });
        buttonLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                oAuthLogin.logout(context);
                Toast.makeText(MainActivity.this, "로그아웃 완료", Toast.LENGTH_SHORT).show();
            }
        });
profile
Hello World
post-custom-banner

0개의 댓글