android Twitter第三方登陆

您所在的位置:网站首页 安卓登入推特教程 android Twitter第三方登陆

android Twitter第三方登陆

2024-07-07 19:34| 来源: 网络整理| 查看: 265

创建app:https://apps.twitter.com 官方DEMO:https://github.com/twitter-archive/twitter-kit-android/wiki/Log-In-with-Twitter

1、接入的第一步还是要在官方后台创建一个app,按照提示一步步添加,此过程就是为了得到key和secret,别忘了callback_url也要添加

2、开始集成: 在app/build.gradle里添加 dependencies { compile ‘com.twitter.sdk.android:twitter-core:3.1.1’ compile ‘com.twitter.sdk.android:tweet-composer:3.1.1’ } 如果需要获取用户的信息的时候,这里实际还用到了compile ‘com.google.code.gson:gson:2.7’,

初始化 Twitter.initialize(this);放在你的 application的onCreate里就行

官方给出的设置默认的配置信息,还可按如下设置 TwitterConfig config = new TwitterConfig.Builder(this) .logger(new DefaultLogger(Log.DEBUG)) .twitterAuthConfig(new TwitterAuthConfig(“CONSUMER_KEY”, “CONSUMER_SECRET”)) .debug(true) .build(); Twitter.initialize(config); 注意:自己实测,此方法根本行不通, 最终是通过第一种初始化,这里你会发现好像初始化和key/secret没啥关系,难道是后续的登陆接口用到,带着疑问又继续集成,

登陆

使用官方定义的登陆按钮 loginButton = (TwitterLoginButton) findViewById(R.id.login_button); 登陆监听 loginButton.setCallback(new Callback() { @Override public void success(Result result) { // Do something with result, which provides a TwitterSession for making API calls }

@Override public void failure(TwitterException exception) { // Do something on failure } });

使用自己的按钮 mTwitterAuthClient = new TwitterAuthClient(); mTwitterAuthClient.authorize(activity, new Callback() { @Override public void success(Result result) { TwitterAuthToken authToken = result.data.getAuthToken();

String token = authToken.token; String tokenSecret = authToken.secret; String userName = result.data.getUserName(); long userId = result.data.getUserId(); getTwitterUserInfo(userId); Log.i("main","authorize.token="+token); Log.i("main","authorize.tokenSecret="+tokenSecret); Log.i("main","authorize.userName="+userName); Log.i("main","authorize.userId="+userId+""); } @Override public void failure(TwitterException exception) { exception.printStackTrace(); Log.i("main","authorize.failure="+exception.toString()); mDologinResult.doLoginFail(); } });

结果返回 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data);

// Pass the activity result to the login button. loginButton.onActivityResult(requestCode, resultCode, data);

}

获取用户信息 private static void getTwitterUserInfo(final long userId){ // AccessToken.getCurrentAccessToken() final TwitterSession activeSession = TwitterCore.getInstance().getSessionManager().getActiveSession(); final String twitterSecret = activeSession.getAuthToken().secret; final String twitterToken = activeSession.getAuthToken().token; MyTwitterApiClient client = new MyTwitterApiClient(activeSession); client.getCustomService().show(userId).enqueue(new Callback() { @Override public void success(Result result) { User data = result.data; // data.name,data.profileImageUrl Log.i(“main”,“authorize.getTwitterUserInfo=”+data.name+“,url=”+data.profileImageUrl); }

@Override public void failure(TwitterException e) { e.printStackTrace(); Log.i("main","authorize.getTwitterUserInfo.failure="+e.getMessage()); mDologinResult.doLoginFail(); } }); }

static class MyTwitterApiClient extends TwitterApiClient { public MyTwitterApiClient(TwitterSession session) { super(session); }

/** * Provide CustomService with defined endpoints */ public CustomService getCustomService() { return getService(CustomService.class); } // example users/show service endpoint interface CustomService { @GET("/1.1/users/show.json") Call show(@Query("user_id") long id); } }

以上登陆的接口官方给的都挺详细,看着好像一切很顺利,好了开始测试,发现一直登陆失败,nnd,到底哪里出了问题,困扰了好久想想还是因为key和secret一直没配置好的问题,反反复复的看官方的文档和api,结果还是一直返回授权失败,和官方给的demo相比较代码的接入上也没有任何问题,最后还是仔细看了他们的demo才发现了问题,原来他的demo里多了一个文件twitter.properties里面就有key和secret的配置信息啥的,这…太nm坑了,官方文档从头到尾都没提到过这么文件,好吧,加上试试看吧,日了,成功了,

最后贴上改文件的内容 twitterConsumerKey=自己的key twitterConsumerSecret=自己的secret



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3