腾讯微博(授权,获取个人信息,分享)


首先加入腾讯的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