只要五分钟,让你成功接入Twitter的第三方登录


第一步:

依赖Twitter的依赖库

首先在整个项目的build.gradle中的 repositories 里添加maven如下:

repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
    jcenter()
}

然后在模块的build.gradle中添加maven库并引入sdk:

repositories {
    mavenCentral()
    maven { url 'https://maven.fabric.io/public' }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    ...
    compile('com.twitter.sdk.android:twitter-core:1.3.1@aar') {
        transitive = true;
    }
}

第二步:封装代码平台

package com.cueme.third.twitter;

import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

import com.cueme.third.Platform;
import com.cueme.third.ThirdLogin;
import com.nomad.instagramlogin.InstaLogin;
import com.twitter.sdk.android.core.Callback;
import com.twitter.sdk.android.core.Result;
import com.twitter.sdk.android.core.TwitterAuthConfig;
import com.twitter.sdk.android.core.TwitterAuthToken;
import com.twitter.sdk.android.core.TwitterCore;
import com.twitter.sdk.android.core.TwitterException;
import com.twitter.sdk.android.core.TwitterSession;
import com.twitter.sdk.android.core.identity.TwitterAuthClient;

import io.fabric.sdk.android.Fabric;

/**
 * Created by puya on 2016/12/28.
 */

public class TwitterPlaform extends Platform {

    private Activity mContext;
    private static final String TWITTER_KEY = "app_id";
    private static final String TWITTER_SECRET = "app_srcret";
    private  TwitterAuthClient twitterAuthClient;

    public TwitterPlaform(Activity context) {
        super(context);
        mContext = context;
        TwitterAuthConfig authConfig =
                new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
        Fabric.with(mContext, new TwitterCore(authConfig));
        twitterAuthClient = new TwitterAuthClient();
    }

    public void login(){
        twitterAuthClient.authorize(mContext, new Callback() {
            @Override
            public void success(Result result) {
                //登录成功
                Toast.makeText(mContext,"login.........",Toast.LENGTH_SHORT).show();
                String authToken = String.valueOf(result.data.getAuthToken());
                String userId = result.data.getUserId()+"";
                if (authToken !=null && userId != null){
                  //处理登录后的逻辑
                }

            }

            @Override
            public void failure(TwitterException e) {
                //授权失败
                Toast.makeText(mContext,"login fail",Toast.LENGTH_SHORT).show();
            }
        });
    }

    public TwitterAuthClient getTwitterAuthClient() {
        return twitterAuthClient;
    }
}


第三步骤:调用

//创建平台

twitterPlaform = new TwitterPlaform(this);

//调用方法

twitterPlaform.login();

//twitter的回调
if(requestCode == TwitterAuthConfig.DEFAULT_AUTH_REQUEST_CODE ){
    Toast.makeText(this,"call back twitter",Toast.LENGTH_SHORT).show();
    twitterPlaform.getTwitterAuthClient().onActivityResult(requestCode,resultCode,data);
}






你可能感兴趣的:(只要五分钟,让你成功接入Twitter的第三方登录)