我给2个类的代码一个是OAuthctivity.java
这个是认证界面的package org.BlackXJian.MicroB;import org.BlackXJian.MicroB.pojo.User;
import org.BlackXJian.MicroB.util.OAuth;import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import android.net.Uri;public class OAuthActivity extends Activity{
private static final String TAG = "OAuthActivity";
private String callBackUrl="blackxjian://OAuthActivity";
private CommonsHttpOAuthConsumer httpOauthConsumer;
private OAuthProvider httpOauthprovider;
OAuth oauth=null;
protected void onCreate (Bundle saveInstanceState)
{
super.onCreate(saveInstanceState);
this.setContentView(R.layout.activity_oauth);
View dialogView = View.inflate(this, R.layout.oauth_dialog, null);
Dialog dialog =new Dialog(this,R.style.oauth_style);
dialog.setContentView(dialogView);
dialog.show();
Button oauth_start =(Button)dialogView.findViewById(R.id.oauth_start);
oauth_start.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(OAuthActivity.this, "认证", Toast.LENGTH_SHORT).show();
oauth=OAuth.getInstance();
//Toast.makeText(OAuthActivity.this, "1", Toast.LENGTH_SHORT).show();
oauth.RequestAccessToken(OAuthActivity.this, callBackUrl);
//Toast.makeText(OAuthActivity.this, "2", Toast.LENGTH_SHORT).show();
}
});
}
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
User user=oauth.GetAccessToken(intent);
Log.i(TAG,"------user-------"+user.toString());
}
}另一个是认证类,申请 新浪 认证返回 界面的
OAuth.javapackage org.BlackXJian.MicroB.util;import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.SortedSet;import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;import org.BlackXJian.MicroB.pojo.*;import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;public class OAuth {
private CommonsHttpOAuthConsumer httpOauthConsumer;
private OAuthProvider httpOauthprovider;
public String consumerKey;
public String consumerSecret;
private static OAuth instance =null;
public static OAuth getInstance()
{
if(instance==null)
{
instance=new OAuth();
}
return instance;
}
public OAuth()
{
// 第一组:(App Key和App Secret)
// 这组参数就是本系列文本第一篇提到的建一个新的应用获取App Key和App Secret。
//wode
//this("1862639130","aa7744f294cab3ad66d8b45866d2991e");
//guannande
this("1985963002","08c44fb802b4872856976c8d990b8fb1");
//别人的 this("30632531","f539cb169860ed99cf8c1861c5da34f6");
Log.d("OAuth", "OAuth succuss");
}
public OAuth(String consumerKey,String consumerSecret)
{
this.consumerKey=consumerKey;
this.consumerSecret=consumerSecret;
}
public Boolean RequestAccessToken(Activity activity,String callBackUrl){
Boolean ret=false;
try{
httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey,consumerSecret);
Log.d("consumerKey", consumerKey);
Log.d("consumerSecret", consumerSecret);
httpOauthprovider = new DefaultOAuthProvider("http://api.t.sina.com.cn/oauth/request_token",
"http://api.t.sina.com.cn/oauth/access_token",
"http://api.t.sina.com.cn/oauth/authorize");
String authUrl = httpOauthprovider.retrieveRequestToken(httpOauthConsumer, callBackUrl);
Log.d("authUrl",authUrl);
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
ret=true;
}catch(Exception e){
}
return ret;
}
public User GetAccessToken(Intent intent){
User user=null;
Uri uri = intent.getData();
String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
try {
httpOauthprovider.setOAuth10a(true);
httpOauthprovider.retrieveAccessToken(httpOauthConsumer,verifier);
} catch (OAuthMessageSignerException ex) {
ex.printStackTrace();
} catch (OAuthNotAuthorizedException ex) {
ex.printStackTrace();
} catch (OAuthExpectationFailedException ex) {
ex.printStackTrace();
} catch (OAuthCommunicationException ex) {
ex.printStackTrace();
}
SortedSet<String> user_id= httpOauthprovider.getResponseParameters().get("user_id");
String userId=user_id.first();
String userKey = httpOauthConsumer.getToken();
String userSecret = httpOauthConsumer.getTokenSecret();
user=new User();
user.setUser_id(userId);
user.setToken(userKey);
user.setToken_secret(userSecret);
return user;
}
public HttpResponse SignRequest(String token,String tokenSecret,String url,List params)
{
HttpPost post = new HttpPost(url);
//HttpClient httpClient = null;
try{
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//关闭Expect:100-Continue握手
//100-Continue握手需谨慎使用,因为遇到不支持HTTP/1.1协议的服务器或者代理时会引起问题
post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
return SignRequest(token,tokenSecret,post);
}
public HttpResponse SignRequest(String token,String tokenSecret,HttpPost post){
httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey,consumerSecret);
httpOauthConsumer.setTokenWithSecret(token,tokenSecret);
HttpResponse response = null;
try {
httpOauthConsumer.sign(post);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
//取得HTTP response
try {
response = new DefaultHttpClient().execute(post);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}}
这是LOG,但是 我点了授权 按钮 ,他始终不返回 新浪的界面 ,我多点了几下,然后 就没反应了android新浪微博OAuth移动开发
这个是认证界面的package org.BlackXJian.MicroB;import org.BlackXJian.MicroB.pojo.User;
import org.BlackXJian.MicroB.util.OAuth;import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;
import android.net.Uri;public class OAuthActivity extends Activity{
private static final String TAG = "OAuthActivity";
private String callBackUrl="blackxjian://OAuthActivity";
private CommonsHttpOAuthConsumer httpOauthConsumer;
private OAuthProvider httpOauthprovider;
OAuth oauth=null;
protected void onCreate (Bundle saveInstanceState)
{
super.onCreate(saveInstanceState);
this.setContentView(R.layout.activity_oauth);
View dialogView = View.inflate(this, R.layout.oauth_dialog, null);
Dialog dialog =new Dialog(this,R.style.oauth_style);
dialog.setContentView(dialogView);
dialog.show();
Button oauth_start =(Button)dialogView.findViewById(R.id.oauth_start);
oauth_start.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Toast.makeText(OAuthActivity.this, "认证", Toast.LENGTH_SHORT).show();
oauth=OAuth.getInstance();
//Toast.makeText(OAuthActivity.this, "1", Toast.LENGTH_SHORT).show();
oauth.RequestAccessToken(OAuthActivity.this, callBackUrl);
//Toast.makeText(OAuthActivity.this, "2", Toast.LENGTH_SHORT).show();
}
});
}
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
User user=oauth.GetAccessToken(intent);
Log.i(TAG,"------user-------"+user.toString());
}
}另一个是认证类,申请 新浪 认证返回 界面的
OAuth.javapackage org.BlackXJian.MicroB.util;import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.SortedSet;import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.protocol.HTTP;import org.BlackXJian.MicroB.pojo.*;import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;public class OAuth {
private CommonsHttpOAuthConsumer httpOauthConsumer;
private OAuthProvider httpOauthprovider;
public String consumerKey;
public String consumerSecret;
private static OAuth instance =null;
public static OAuth getInstance()
{
if(instance==null)
{
instance=new OAuth();
}
return instance;
}
public OAuth()
{
// 第一组:(App Key和App Secret)
// 这组参数就是本系列文本第一篇提到的建一个新的应用获取App Key和App Secret。
//wode
//this("1862639130","aa7744f294cab3ad66d8b45866d2991e");
//guannande
this("1985963002","08c44fb802b4872856976c8d990b8fb1");
//别人的 this("30632531","f539cb169860ed99cf8c1861c5da34f6");
Log.d("OAuth", "OAuth succuss");
}
public OAuth(String consumerKey,String consumerSecret)
{
this.consumerKey=consumerKey;
this.consumerSecret=consumerSecret;
}
public Boolean RequestAccessToken(Activity activity,String callBackUrl){
Boolean ret=false;
try{
httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey,consumerSecret);
Log.d("consumerKey", consumerKey);
Log.d("consumerSecret", consumerSecret);
httpOauthprovider = new DefaultOAuthProvider("http://api.t.sina.com.cn/oauth/request_token",
"http://api.t.sina.com.cn/oauth/access_token",
"http://api.t.sina.com.cn/oauth/authorize");
String authUrl = httpOauthprovider.retrieveRequestToken(httpOauthConsumer, callBackUrl);
Log.d("authUrl",authUrl);
activity.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
ret=true;
}catch(Exception e){
}
return ret;
}
public User GetAccessToken(Intent intent){
User user=null;
Uri uri = intent.getData();
String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
try {
httpOauthprovider.setOAuth10a(true);
httpOauthprovider.retrieveAccessToken(httpOauthConsumer,verifier);
} catch (OAuthMessageSignerException ex) {
ex.printStackTrace();
} catch (OAuthNotAuthorizedException ex) {
ex.printStackTrace();
} catch (OAuthExpectationFailedException ex) {
ex.printStackTrace();
} catch (OAuthCommunicationException ex) {
ex.printStackTrace();
}
SortedSet<String> user_id= httpOauthprovider.getResponseParameters().get("user_id");
String userId=user_id.first();
String userKey = httpOauthConsumer.getToken();
String userSecret = httpOauthConsumer.getTokenSecret();
user=new User();
user.setUser_id(userId);
user.setToken(userKey);
user.setToken_secret(userSecret);
return user;
}
public HttpResponse SignRequest(String token,String tokenSecret,String url,List params)
{
HttpPost post = new HttpPost(url);
//HttpClient httpClient = null;
try{
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//关闭Expect:100-Continue握手
//100-Continue握手需谨慎使用,因为遇到不支持HTTP/1.1协议的服务器或者代理时会引起问题
post.getParams().setBooleanParameter(CoreProtocolPNames.USE_EXPECT_CONTINUE, false);
return SignRequest(token,tokenSecret,post);
}
public HttpResponse SignRequest(String token,String tokenSecret,HttpPost post){
httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey,consumerSecret);
httpOauthConsumer.setTokenWithSecret(token,tokenSecret);
HttpResponse response = null;
try {
httpOauthConsumer.sign(post);
} catch (OAuthMessageSignerException e) {
e.printStackTrace();
} catch (OAuthExpectationFailedException e) {
e.printStackTrace();
} catch (OAuthCommunicationException e) {
e.printStackTrace();
}
//取得HTTP response
try {
response = new DefaultHttpClient().execute(post);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return response;
}}
这是LOG,但是 我点了授权 按钮 ,他始终不返回 新浪的界面 ,我多点了几下,然后 就没反应了android新浪微博OAuth移动开发
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货