"
public class Weibo extends WeiboSupport implements java.io.Serializable {
public static String CONSUMER_KEY = "41199486xx";
public static String CONSUMER_SECRET = "d589738xx1e0026xxce22xx84cf87dxx";
<EditText android:layout_height="wrap_content"
android:text=""
android:layout_width="260dip"
android:id="@+id/account" />
<EditText android:layout_height="wrap_content"
android:text=""
android:layout_width="260dip"
android:id="@+id/password" />
<Button android:layout_height="wrap_content"
android:text="auth"
android:layout_width="wrap_content"
android:id="@+id/authButton" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
androidandroid:layout_width="fill_parent"android:layout_height="fill_parent"
androidandroid:scrollbars="vertical"android:fadingEdge="vertical">
<TextView android:layout_width="fill_parent"
androidandroid:layout_height="wrap_content"android:id="@+id/authResult"
android:paddingTop="5dip"/>
</ScrollView>
public class OAuthVerifier extends OAuthToken {
private static final long serialVersionUID = -8344528374458826291L;
private String verifier;
OAuthVerifier(Response res) throws WeiboException {
this(res.asString());
}
OAuthVerifier(String str) {
super(str);
String[] results = str.split(":");
if(results.length >= 3) {
verifier =results[2].substring(1, 7);
} else {
verifier = "";
}
}
public OAuthVerifier(String token,String tokenSecret) {
super(token, tokenSecret);
}
/**
*
* @return verifier
* @since Weibo4android
*/
public String getVerifier() {
return verifier;
}
}
然后修改androidexamples包下的AndroidExample类。初始化界面元素,设置authButton点击时的事件处理。
/* 初始化界面元素 */
ButtonbeginOuathBtn = (Button)findViewById(R.id.authButton);
final EditText accountInput= (EditText) findViewById(R.id.account);
final EditTextpasswordInput = (EditText) findViewById(R.id.password);
/* oauth认证按钮的点击事件 */
beginOuathBtn.setOnClickListener(newButton.OnClickListener()
{
public void onClick( View v )
{
Weiboweibo = OAuthConstant.getInstance().getWeibo(); // init weibo object
RequestTokenrequestToken;
try {
requestToken = weibo.getOAuthRequestToken();
OAuthConstant.getInstance().setRequestToken(requestToken);
String username =accountInput.getText().toString();
String password =passwordInput.getText().toString();
OAuthVerifier oauthVerifier = weibo.getOauthVerifier(username,password); // get verifier
String verifier = oauthVerifier.getVerifier();
AccessToken accessToken =requestToken.getAccessToken(verifier); // get access token
OAuthConstant.getInstance().setAccessToken(accessToken);
TextView textView = (TextView) findViewById(R.id.authResult);
textView.setText("得到AccessToken的key和Secret,可以使用这两个参数进行授权登录了.\n Access token:\n"
+ accessToken.getToken() + "\n Access token secret:\n" + accessToken.getTokenSecret());
} catch (WeiboException e) {
e.printStackTrace();
}
}
});
在src下weibo4android包的Weibo.java里添加getOAuthVerifier方法。
public OAuthVerifiergetOAuthVerifier(Stringusername, Stringpassword) throws WeiboException {
return http.getOAuthVerifier(username,password);
}
在src下weibo4android.http包的HttpClient.java文件里添加如下代码:
private OAuthVerifier oauthVerifier = null;
public OAuthVerifiergetOAuthVerifier(String username, String password) throws WeiboException {
this.oauthVerifier = newOAuthVerifier(httpRequest(authorizationURL,
new PostParameter[]{new PostParameter("userId", username), new PostParameter("passwd", password), new PostParameter("oauth_callback", "json")}
,true)); // callback = json isimportant!
return (OAuthVerifier) this.oauthVerifier;
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。
"
学习了!
######这个方法现在不行了吧,401:Authentication credentials were missing or incorrect.######如果第一次输入错误~第二次输入后为什么还是报错呢