腾讯微博开放平台的小项目 关于OAUTH授权
老是获取不了授权,不知道哪里做错了,请大神指点Constant接口
package earl.oauth;
public interface Constant {
public static final String CALLBACK_SCHEME ="x-oauthflow";
public static final String CALLBACK_HOST ="callback";
public static final String CALLBACK_URL = CALLBACK_SCHEME +"://" +CALLBACK_HOST;
public static final String CONSUMER_KEY = "801442441";
public static final String CONSUMER_SECRET ="9c4815705b9ba21b10ccf3dab5606b37";
public static final String AUTHORIZE_URL ="http://open.t.qq.com/cgi-bin/authorize";
public static final String ACCESS_URL="http://open.t.qq.com/cgi-bin/access_token";
public static final String REQUEST_URL="http://open.t.qq.com/cgi-bin/request_token";
}
主activity
package earl.oauth;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.tencent.weibo.beans.OAuth;
import com.tencent.weibo.utils.OAuthClient;
import earl.activity.R;
public class OAuthActivity extends Activity {
private OAuth oAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 初始化对象,由于是移动应用,所以设置回调函数会null
oAuth = new OAuth(Constant.CALLBACK_URL);
// 设置app的key和密钥
oAuth.setOauth_consumer_key(Constant.CONSUMER_KEY);
oAuth.setOauth_consumer_secret(Constant.CONSUMER_SECRET);
oAuth.setOauth_token(Constant.REQUEST_URL);
Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 异步启动授权任务
new OAuthRequestTokenTask().execute();
}
});
}
class OAuthRequestTokenTask extends AsyncTask<Void, Integer, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
OAuthClient oAuthClient = new OAuthClient();
System.out.println("未请求token之前: " + oAuth.getOauth_token());
// 发送请求,并用oAuth获得返回值
oAuth = oAuthClient.requestToken(oAuth);
System.out.println("请求token后:" + oAuth.getOauth_token());
Intent intent = new Intent(OAuthActivity.this,
WebViewActivity.class);
intent.putExtra("accessToken",Constant.AUTHORIZE_URL+ "?oauth_token="
+ "e4ea7beeb82a44f6a4137c0f8fcaa794");
intent.putExtra("accessTokenSecret",
oAuth.getOauth_token_secret());
startActivityForResult(intent, 1);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
跳转的activity
package earl.oauth;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;
public class WebViewActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
//实例化WebView对象
WebView webview = new WebView(this);
//设置WebView属性,能够执行Javascript脚本
webview.getSettings().setJavaScriptEnabled(true);
//加载需要显示的网页
webview.loadUrl( intent.getStringExtra("accessToken").toString() );
//设置Web视图
setContentView(webview);
}
}
我看腾讯的API看不明白,所以做得糊里糊涂的。还有怎样获取返回的授权验证码。如果可以,请各位大神给出源码 腾讯微博开放平台 OAUTH授权 --------------------编程问答-------------------- 建议直接拷贝demo
补充:移动开发 , Android