騰訊微博(授權,獲取個人信息,分享)


首先加入騰訊的jar包(自行下載)。


功能實現類:

public class MainActivity extends Activity {

    private static String TAG="Weibo.class";
    /*
     * 申請APP KEY的具體介紹,可參見 
     * http://wiki.open.t.qq.com/index.php/應用接入指引
     * http://wiki.open.t.qq.com/index.php/騰訊微博移動應用接入規范#.E6.8E.A5.E5.85.A5.E6.B5.81.E7.A8.8B
     */
    //!!!請根據您的實際情況修改!    認證成功后瀏覽器會被重定向到這個url中  必須與注冊時填寫的一致
    private String redirectUri="http://www.cnblogs.com/maxinliang/";                    
    //!!!請根據您的實際情況修改!    換為您為自己的應用申請到的APP KEY
    private String clientId = "801228135"; 
    //!!!請根據您的實際情況修改!    換為您為自己的應用申請到的APP SECRET
    private String clientSecret="8498b13f93413d87d20e334b25c6fab0";
    private OAuthV2 oAuth;
    TAPI tAPI;
    String response;
    UserAPI userAPI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.oauthv2);
        //對oAuth實例化,設置相關屬性
        oAuth=new OAuthV2(redirectUri);
        oAuth.setClientId(clientId);
        oAuth.setClientSecret(clientSecret);
        //關閉OAuthV2Client中的默認開啟的QHttpClient。
        OAuthV2Client.getQHttpClient().shutdownConnection();
      //詳見StrictMode文檔--->我用的是sdk4.1,sdk4.1不讓在主線程進行http請求,加入下面兩行代碼,就可以了!
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
        
        //按鈕監聽方法的實例
        OnClickListener listener=new OnClickListener()
        {
            public void onClick(View v){
               Intent intent;
               switch (v.getId()){
               
               //授權方法
                    case R.id.btn1:
                        Log.i(TAG, "-------------Step1: 授權方法--------------");
                         //創建Intent,使用WebView讓用戶授權
                        intent = new Intent(MainActivity.this, OAuthV2AuthorizeWebView.class);
                        intent.putExtra("oauth", oAuth);
                        startActivityForResult(intent,2);   
                        break;
                //獲取用戶信息
                    case R.id.btn2:
                        Log.i(TAG, "-------------Step2: 獲取用戶信息方法--------------");
                        userAPI=new UserAPI(OAuthConstants.OAUTH_VERSION_2_A);
                        try {
                            //調用QWeiboSDK獲取用戶信息
                            response=userAPI.info(oAuth, "json");
                            System.out.println(response);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        userAPI.shutdownConnection();
                        break;
                //發布文字微博
                    case R.id.button1:
                        Log.i(TAG, "-------------Step3: 發布文字微博--------------");
                        System.out.println("發布文字微博");
                        tAPI= new TAPI(OAuthConstants.OAUTH_VERSION_2_A);
                        try {
                            response=tAPI.add(oAuth, "json", "Android客戶端文字微博2", "127.0.0.1");
                            System.out.println(response+"\n");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        tAPI.shutdownConnection();
                        break;
                 //發布帶圖片的微博
                    case R.id.button2:
                        Log.i(TAG, "-------------Step4: 發布帶圖片的微博--------------");
                        System.out.println("發布圖片微博");
                        tAPI= new TAPI(OAuthConstants.OAUTH_VERSION_2_A);
                        try {
                            File fileDir=new File("/sdcard/qweibosdk2");
                            if(!fileDir.exists()) fileDir.mkdirs();
                            File file=new File("/sdcard/qweibosdk2/logo_QWeibo.jpg");
                            if(!file.exists()){
                                file.createNewFile();
                                InputStream inputStream=MainActivity.class.getResourceAsStream("/res/drawable-hdpi/logo_qweibo.jpg");
                                FileOutputStream fileOutputStream=new FileOutputStream(file);
                                byte[] buf=new byte[1024];
                                int ins;
                                while ((ins=inputStream.read(buf))!=-1) {
                                    fileOutputStream.write(buf,0,ins);
                                }
                                inputStream.close();
                                fileOutputStream.close();
                            }
                            String picPath="/sdcard/qweibosdk2/logo_QWeibo.jpg";
                            response=tAPI.addPic(oAuth,  "json",  "Android客戶端帶圖的文字微博", "127.0.0.1", picPath);
                            System.out.println(response+"\n");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        tAPI.shutdownConnection();
                        break;
                }
            }
        };
        
        //四個按鈕的聲明
        Button btn1=(Button)findViewById(R.id.btn1);
        Button btn2=(Button)findViewById(R.id.btn2);
        Button btn3=(Button)findViewById(R.id.button1);
        Button btn4=(Button)findViewById(R.id.button2);
        //為四個按鈕設置監聽方法
        btn1.setOnClickListener(listener);
        btn2.setOnClickListener(listener);
        btn2.setOnClickListener(listener);
        btn3.setOnClickListener(listener);
       
    }
    
    /*
     * 通過讀取OAuthV2AuthorizeWebView返回的Intent,獲取用戶授權信息
     */
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        if (requestCode==2) {
            if (resultCode==OAuthV2AuthorizeWebView.RESULT_CODE)    {
                oAuth=(OAuthV2) data.getExtras().getSerializable("oauth");
                if(oAuth.getStatus()==0)
                    Toast.makeText(getApplicationContext(), "登陸成功", Toast.LENGTH_SHORT).show();
            }
        }
    }
    
    public void onBackPressed() {
        finish();
    }
}
oauthv2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="V2授權" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="獲取用戶信息" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="發布文字文博" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="發布帶圖片的微博" />

</LinearLayout>

權限聲明和activity聲明

<!-- 網絡權限 -->
    <uses-permission android:name="android.permission.INTERNET" />
    <!-- com.tencent.weibo。demo 中的 WeiBoAPIV1Activity 和 WeiBoAPIV2Activity 在測試上傳帶圖片微博使用 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>




 <!-- OAuth Version 2. 使用  WebView 輔助進行ImplicitGrant方式授權必須 -->
        <activity
            android:name="com.tencent.weibo.webview.OAuthV2AuthorizeWebView"
            android:label="@string/app_name" >
        </activity>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM