Java使用新浪微博API通過賬號密碼方式登陸微博的實例
今天下了個新浪微博的API研究研究,目前實現(xiàn)了發(fā)布微博功能,包括帶圖片的微博。為了安全,新浪微博的API中并沒有提供用微博帳號密碼登錄的功能,而是采用OAuth授權(quán),用戶通過瀏覽器訪問新浪網(wǎng)站登錄,登錄成功后,瀏覽器再返回key和secret給程序。
main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/login" android:text="登錄" /> <EditText android:id="@+id/status" android:layout_width="fill_parent" android:layout_height="300sp" android:hint="輸入微博消息" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/send" android:text="發(fā)布" /> </LinearLayout>
一個登錄按鈕,一個輸入框,一個發(fā)布按鈕
因為要接收瀏覽器返回的數(shù)據(jù),所以,AndroidManifest.xml注冊Activity的時候要加個Intent-Filter
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pocketdigi.weibo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Main" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="sina" android:host="weibo" /> <!-- 監(jiān)控sina://weibo這樣的地址 --> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.INTERNET"></uses-permission> </manifest>
intent-filter必須分成兩段寫,如果合在一起寫,就啟動不了了。
為了簡便,直接把新浪Sample里的OAuthConstant類拷過來:
package weibo4android.androidexamples; import weibo4android.Weibo; import weibo4android.http.AccessToken; import weibo4android.http.RequestToken; public class OAuthConstant { private static Weibo weibo=null; private static OAuthConstant instance=null; private RequestToken requestToken; private AccessToken accessToken; private String token; private String tokenSecret; private OAuthConstant(){}; public static synchronized OAuthConstant getInstance(){ if(instance==null) instance= new OAuthConstant(); return instance; } public Weibo getWeibo(){ if(weibo==null) weibo= new Weibo(); return weibo; } public AccessToken getAccessToken() { return accessToken; } public void setAccessToken(AccessToken accessToken) { this.accessToken = accessToken; this.token=accessToken.getToken(); this.tokenSecret=accessToken.getTokenSecret(); } public RequestToken getRequestToken() { return requestToken; } public void setRequestToken(RequestToken requestToken) { this.requestToken = requestToken; } public String getToken() { return token; } public void setToken(String token) { this.token = token; } public String getTokenSecret() { return tokenSecret; } public void setTokenSecret(String tokenSecret) { this.tokenSecret = tokenSecret; } }
接下來就是最關鍵的主程序:
package com.pocketdigi.weibo; import java.io.File; import weibo4android.Weibo; import weibo4android.WeiboException; import weibo4android.http.AccessToken; import weibo4android.http.RequestToken; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class Main extends Activity { /** Called when the activity is first created. */ String key = "", secret = ""; Button login,send; EditText status; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); System.setProperty("weibo4j.oauth.consumerKey", "3997936609"); System.setProperty("weibo4j.oauth.consumerSecret", "8bc9e3bfd6ae8e3b2b8bda9079918950"); //設置在新浪應用開放平臺申請的應用的key和secret login=(Button)findViewById(R.id.login); send=(Button)findViewById(R.id.send); status=(EditText)findViewById(R.id.status); login.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub login(); //登錄 }}); send.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub String text=String.valueOf(status.getText()); Weibo weibo = new Weibo(); weibo.setToken(key,secret); try { //weibo.updateStatus(text); //只發(fā)文字 File f=new File("/sdcard/wallpaper/129567208597069400.jpg"); weibo.uploadStatus(text,f ); //發(fā)文字+圖片,這里需要導入commons-httpclient-3.0.1.jar,自己網(wǎng)上下 //在實際項目上,最好放Thread里,因為按下去的時候按鈕會卡 } catch (WeiboException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); //啟動時執(zhí)行檢測是否來自網(wǎng)頁登錄返回 //如果是,獲取key和secret //否則讀取SharedPreferences //若得不到key和secret,直接跳轉(zhuǎn)登錄 Uri uri = this.getIntent().getData(); if (uri != null) { //如果是瀏覽器返回 try { RequestToken requestToken = OAuthConstant.getInstance() .getRequestToken(); AccessToken accessToken = requestToken.getAccessToken(uri .getQueryParameter("oauth_verifier")); OAuthConstant.getInstance().setAccessToken(accessToken); // 保存 Editor sharedata = getSharedPreferences("WeiBo", 0).edit(); sharedata.putString("key", accessToken.getToken()); sharedata.putString("secret", accessToken.getTokenSecret()); sharedata.commit(); key = accessToken.getToken(); secret = accessToken.getTokenSecret(); } catch (WeiboException e) { e.printStackTrace(); } } else { //如果是用戶自己啟動 SharedPreferences settings = getSharedPreferences("WeiBo", 0); key = settings.getString("key", ""); secret = settings.getString("secret", ""); } if (key.equals("") || secret.equals("")) { Toast.makeText(this, "尚未登錄", Toast.LENGTH_LONG).show(); login(); //跳轉(zhuǎn)到瀏覽器登錄 } } public void login(){ Weibo weibo = OAuthConstant.getInstance().getWeibo(); RequestToken requestToken; try { requestToken =weibo.getOAuthRequestToken("sina://weibo"); //為了避免與同類應用沖突,還是自己改下URI吧 Uri uri2 = Uri.parse(requestToken.getAuthenticationURL()+ "&from=xweibo"); OAuthConstant.getInstance().setRequestToken(requestToken); startActivity(new Intent(Intent.ACTION_VIEW, uri2)); } catch (WeiboException e) { e.printStackTrace(); } } }
發(fā)圖片需要導入commons-httpclient-3.0.1.jar,否則啟動報錯,當然weibo4android-1.2.0.jar是不可少的
相關文章
spring 自動注入AutowiredAnnotationBeanPostProcessor源碼解析
這篇文章主要介紹了spring自動注入AutowiredAnnotationBeanPostProcessor源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03Java中Spring Boot+Socket實現(xiàn)與html頁面的長連接實例詳解
這篇文章主要介紹了Java中Spring Boot+Socket實現(xiàn)與html頁面的長連接實例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07Mybatis的TypeHandler實現(xiàn)數(shù)據(jù)加解密詳解
這篇文章主要介紹了Mybatis基于TypeHandler實現(xiàn)敏感數(shù)據(jù)加密詳解,Typehandler是mybatis提供的一個接口,通過實現(xiàn)這個接口,可以實現(xiàn)jdbc類型數(shù)據(jù)和java類型數(shù)據(jù)的轉(zhuǎn)換,需要的朋友可以參考下2024-01-01區(qū)分java中String+String和String+char
這篇文章主要向大家詳細區(qū)分了java中String+String和String+char,感興趣的小伙伴們可以參考一下2016-01-01