Android Open Source - Peterson Tencent Weibo Authorizer






From Project

Back to project page Peterson.

License

The source code is released under:

GNU General Public License

If you think the Android project Peterson listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*******************************************************************************
 * Copyright (c) 2013 Zheng Sun.//w  w w.ja  va  2 s .c  om
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *     Zheng Sun - initial API and implementation
 ******************************************************************************/

package tv.huohua.peterson.social;

import com.tencent.weibo.oauthv2.OAuthV2;
import com.tencent.weibo.webview.OAuthV2AuthorizeWebView;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;

public class TencentWeiboAuthorizer implements ISocialAuthorizer {
    public static interface AuthorizationListener {
        void onAuthorizationFinished(final TencentWeiboAuthorizer authorizer, final OAuthV2 oAuthV2);
    }

    public static class TencentWeiboAuthHandler {
        static final public int REQUEST_CODE_AUTHORIZATION = 213274384;

        private TencentWeiboAuthorizer authorizer;

        public TencentWeiboAuthHandler(final TencentWeiboAuthorizer authorizer) {
            this.authorizer = authorizer;
        }

        public void authorizeCallBack(final int requestCode, final int resultCode, final Intent data) {
            if (requestCode == REQUEST_CODE_AUTHORIZATION) {
                if (resultCode == OAuthV2AuthorizeWebView.RESULT_CODE) {
                    final OAuthV2 oAuthV2 = (OAuthV2) data.getExtras().getSerializable("oauth");
                    if (oAuthV2.getStatus() == 0) {
                        TencentWeiboAccessTokenKeeper.keepAccessToken(authorizer.activity, oAuthV2);
                    }
                    if (authorizer.authorizationListener != null) {
                        authorizer.authorizationListener.onAuthorizationFinished(authorizer, oAuthV2);
                    }
                }
            }
        }
    }

    public static OAuthV2 getOAuthV2Token(final Context context) {
        return TencentWeiboAccessTokenKeeper.readAccessToken(context);
    }

    public static boolean isAuthed(final Context context) {
        final OAuthV2 oAuthV2 = TencentWeiboAccessTokenKeeper.readAccessToken(context);
        return (oAuthV2 != null);
    }

    public static void unauth(final Context context) {
        TencentWeiboAccessTokenKeeper.clear(context);
    }

    private final Activity activity;
    private AuthorizationListener authorizationListener;
    private final String clientId;
    private final String clientSecret;
    private final String redirectUri;

    public TencentWeiboAuthorizer(final Activity activity, final String clientId, final String clientSecret,
            final String redirectUri) {
        this.activity = activity;
        this.clientId = clientId;
        this.clientSecret = clientSecret;
        this.redirectUri = redirectUri;
    }

    public Activity getActivity() {
        return activity;
    }

    public AuthorizationListener getAuthorizationListener() {
        return authorizationListener;
    }

    public String getClientId() {
        return clientId;
    }

    public String getClientSecret() {
        return clientSecret;
    }

    public OAuthV2 getOAuthV2Token() {
        return getOAuthV2Token(activity);
    }

    public String getRedirectUri() {
        return redirectUri;
    }

    @Override
    public boolean isAuthed() {
        return isAuthed(activity);
    }

    public void setAuthorizationListener(final AuthorizationListener authorizationListener) {
        this.authorizationListener = authorizationListener;
    }

    public TencentWeiboAuthHandler startAuth() {
        final OAuthV2 oAuth = new OAuthV2(redirectUri);
        oAuth.setClientId(clientId);
        oAuth.setClientSecret(clientSecret);

        final Intent intent = new Intent(activity, OAuthV2AuthorizeWebView.class);
        final TencentWeiboAuthHandler handler = new TencentWeiboAuthHandler(this);
        intent.putExtra("oauth", oAuth);
        activity.startActivityForResult(intent, TencentWeiboAuthHandler.REQUEST_CODE_AUTHORIZATION);
        return handler;
    }

    @Override
    public void unauth() {
        unauth(activity);
    }
}




Java Source Code List

tv.huohua.peterson.api.AbsApi.java
tv.huohua.peterson.api.AbsListApi.java
tv.huohua.peterson.api.ApiCallResponse.java
tv.huohua.peterson.misc.ApplicationUtils.java
tv.huohua.peterson.misc.DeviceUtils.java
tv.huohua.peterson.misc.IOUtils.java
tv.huohua.peterson.misc.JavaLangUtils.java
tv.huohua.peterson.misc.Pair.java
tv.huohua.peterson.network.HttpRequest.java
tv.huohua.peterson.network.NetworkMgr.java
tv.huohua.peterson.network.NetworkUtils.java
tv.huohua.peterson.social.ISocialAuthorizer.java
tv.huohua.peterson.social.TencentWeiboAccessTokenKeeper.java
tv.huohua.peterson.social.TencentWeiboAuthorizer.java
tv.huohua.peterson.social.WeiboAccessTokenKeeper.java
tv.huohua.peterson.social.WeiboApiCaller.java
tv.huohua.peterson.social.WeiboAuthorizer.java
tv.huohua.peterson.view.FixedAspectRatioImageView.java
tv.huohua.peterson.view.FixedAspectRatioRelativeLayout.java
tv.huohua.peterson.view.HHListView.java
tv.huohua.peterson.view.HorizontalPagerListener.java
tv.huohua.peterson.view.HorizontalPager.java