其实新浪API的说明文档已经说的很明白了,demo里面注释也很清晰。
官方SDK下载地址:https://github.com/sinaweibosdk/weibo_android_sdk
我直接把我的代码贴出来吧。
说明:授权方式有三种,官方指南上说推荐第一种,我就用了第一种SSO方式。
一 场景说明:
点击分享到微博按钮,开始进行授权验证,授权成功后自动进入分享界面。我这里设置发布文字和图片。图片是根据前面的Activity传过来的图片名,从sd卡中加载的。
二 操作说明:
1.下载官方SDK,修改语言为UTF8,导入eclipse
2.在自己的项目中添加library:WeiboSDK
3.将WeiboSDKDemo的jar包 weibosdkscore.jar复制添加到自己的项目中,也就是说一共有两个weibo相关的jar包:weibosdk.jar weibosdkscore.jar
4.将Demo中Constants.java复制到自己的项目中,修改其中的APP_KEY和REDIRECT_URL。
APP_KEY 与 开发者平台上项目的key一致。
REDIRECT_URL只要与自己在开发者平台上设置的一样就行。建议使用默认的url:https://api.weibo.com/oauth2/default.html
由于采用sso授权,因此App Secret 不需要写进项目中。
5.将Demo中AccessTokenKeeper.java 复制到自己工程中,内容无需更改。这个是用来存储授权拿到的token信息的,每次授权之前会检查此账号是否token有效,授权
后会更新。
6.在自己项目中设置点击事件跳转到 ShareMainActivity这个类。
三 当前存在的问题:
1.授权成功后有时候会显示一个白底 Auth2.0的 页面,有时候一闪而过。这个页面怎么跳过让其不显示?
2.同时在应用中授权超过一个微博账号时,授权成功会进入一个选择账号的页面,会让你选择哪一个账号进行分享,但是只有一个账号时也有那个页面只不过一闪而过,
能否让那个页面停留,等候操作呢?因为那个页面有一个添加账号的选项。
四 代码:
1.iread_activity_share.xml
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F5FFFA" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<ImageView
android:id="@+id/iread_read_share_weibo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1"
android:src="@drawable/iread_read_share_weibo" />
<ImageView
android:id="@+id/iread_read_share_wechat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_weight="1"
android:src="@drawable/iread_read_share_wechat" />
</LinearLayout>
<TextView
android:id="@+id/iread_read_share_log"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="31dp"
android:text="" />
</RelativeLayout>
2.ShareMainActivity.java
/*
* Copyright (C) 2010-2013 The SINA WEIBO Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apach