Skip to content

Commit

Permalink
增加豆瓣授权认证
Browse files Browse the repository at this point in the history
  • Loading branch information
colarking committed Oct 11, 2014
1 parent a981f82 commit f7a6707
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 0 deletions.
6 changes: 6 additions & 0 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
</activity>
<activity
android:name="com.iyoudang.matrix.share.AmayaAuthorize"
android:launchMode="singleTask"
>
</activity>
<activity
android:name="com.iyoudang.matrix.share.AmayaDoubanAuth"
android:launchMode="singleTask"
>
</activity>
<activity
Expand Down
30 changes: 30 additions & 0 deletions res/layout/authorize_webview.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">

<WebView
android:id="@+id/authorize_webview_id"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/wait"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/darker_gray"
android:gravity="center" >

<ProgressBar
android:layout_width="70dp"
android:layout_height="70dp"
android:indeterminateDrawable="@drawable/abs__progress_medium_holo" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/amaya_loading"
/>
</LinearLayout>

</FrameLayout>
1 change: 1 addition & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AmayaShare</string>
<string name="amaya_loading">请稍候...</string>
<string name="amaya_auth_no">未授权应用,分享失败</string>
<string name="amaya_auth_fail">授权失败,请稍候重试</string>
<string name="amaya_auth_fail_step_0">授权失败,请检查配置信息是否有误</string>
Expand Down
264 changes: 264 additions & 0 deletions src/com/iyoudang/matrix/share/AmayaDoubanAuth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
package com.iyoudang.matrix.share;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.CookieManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout;
import com.iyoudang.matrix.R;
import com.iyoudang.matrix.share.util.AmayaShare;
import com.iyoudang.matrix.share.util.AmayaShareConstants;
import com.iyoudang.matrix.share.util.AmayaTokenKeeper;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
* 授权webview
*
* @author liyanju1
*
*/
public class AmayaDoubanAuth extends Activity {

private static final String TAG = AmayaDoubanAuth.class.getSimpleName();

private View waitView;


private WebView mWebView;
private static AmayaDoubanAuth authorWebView;

@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.authorize_webview);
authorWebView = this;
Intent intent = getIntent();
mWebView = (WebView) findViewById(R.id.authorize_webview_id);
waitView = findViewById(R.id.wait);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
WebView.enablePlatformNotifications();
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WeiboWebViewClient());

final String url = AmayaShareConstants.AMAYA_DOUBAN_AUTH_URL + "?client_id=" + AmayaShareConstants.AMAYA_DOUBAN_ID + "&redirect_uri=" + AmayaShareConstants.AMAYA_DOUBAN_REDIRECT_URI + "&response_type=code";
mWebView.loadUrl(url);
}

private class WeiboWebViewClient extends WebViewClient {

/***
* 地址改变都会调用
*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, final String url) {
// if (!url.startsWith(AmayaShareConstants.AMAYA_DOUBAN_AUTH_URL) && !isEnter) {
// waitProgress.setVisibility(View.VISIBLE);
// new Thread(new Runnable() {
//
// @Override
// public void run() {
//
// Bundle bundle = getAccessToken(url);
// Message msg = mHandler.obtainMessage();
// msg.obj = bundle;
// msg.sendToTarget();
// }
// }).start();
// return true;
// }
return super.shouldOverrideUrlLoading(view, url);
}

@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);

}

@Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
handler.proceed();
}

@Override
public void onPageStarted(WebView view, final String url, Bitmap favicon) {
if (url.startsWith(AmayaShareConstants.AMAYA_DOUBAN_REDIRECT_URI) ) {
waitView.setVisibility(View.VISIBLE);
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Bundle bundle = getAccessToken(url);
Intent in = new Intent();
in.putExtras(bundle);
AmayaShare.instance().onActivityResult(AmayaDoubanAuth.this,AmayaShareConstants.AMAYA_ACTIVITY_RESULT_DOUBAN,RESULT_OK,in);
finish();
}
}).start();
}
super.onPageStarted(view, url, favicon);
}

@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
waitView.setVisibility(View.GONE);
}
}

public Bundle getAccessToken(String url) {
String code = url.substring(url.indexOf("code=") + 5, url.length());
Log.e(TAG, "getAccessToken()...code : " + code);
Bundle bundle = new Bundle();
HashMap<String,String> params = new HashMap<String, String>();
params.put("client_id", AmayaShareConstants.AMAYA_DOUBAN_ID);
params.put("client_secret", AmayaShareConstants.AMAYA_DOUBAN_SECRET);
params.put("redirect_uri", AmayaShareConstants.AMAYA_DOUBAN_REDIRECT_URI);
params.put("code", code);
params.put("grant_type", "authorization_code");
try {
String s = amayaHttpPost(AmayaShareConstants.AMAYA_DOUBAN_TOKEURL, params);
JSONObject jsonObject = new JSONObject(s);
String access_token = jsonObject.getString("access_token");
String douban_user_name = jsonObject.getString("douban_user_name");
String douban_user_id = jsonObject.getString("douban_user_id");
String expires_in = jsonObject.getString("expires_in");
//String refresh_token = jsonObject.getString("refresh_token");
bundle.putString(AmayaShareConstants.AMAYA_RESULT_ACCESS_TOKEN, access_token);
bundle.putString(AmayaShareConstants.AMAYA_RESULT_EXPIRES_IN, expires_in);
bundle.putString(AmayaShareConstants.AMAYA_RESULT_USER_ID, douban_user_id);
bundle.putString(AmayaShareConstants.AMAYA_RESULT_USER_NAME, douban_user_name);
// bundle.putInt(SNSManager.SNS_TYPE, SNSManager.DOUBAN_TYPE);
AmayaTokenKeeper.saveDoubanToken(this, access_token, expires_in);
String j = amayaHttpGet(AmayaShareConstants.AMAYA_DOUBAN_USER_INFO_URL + douban_user_id);
JSONObject jo = new JSONObject(j);
if(jo != null){
Log.e("amaya","getAccessToken()...180..."+jo.toString());
//{"msg":"uri_not_found","request":"POST \/v2\/user\/74bcaa8d5ba7b9645e35ffa812f72c2c","code":1001}
String img = jo.getString("large_avatar");
if(!TextUtils.isEmpty(img)) bundle.putString(AmayaShareConstants.AMAYA_RESULT_USER_IMG,img);
}

Log.e(TAG, "getAccessToken()...access_token : " + access_token
+ " douban_user_id : " + douban_user_id + " expires_in : "
+ expires_in);
} catch (JSONException e) {
e.printStackTrace();
}
return bundle;
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mWebView != null) {
((RelativeLayout)mWebView.getParent()).removeView(mWebView);
mWebView.stopLoading();
mWebView.clearView();
mWebView.removeAllViews();
mWebView.destroy();
mWebView = null;
}
CookieManager.getInstance().removeAllCookie();
System.gc();
}

public static void finishActivity() {
if(null!= authorWebView)
authorWebView.finish();
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// if (isEnter) {
// return false;
// } else {
// }
return super.onKeyDown(keyCode, event);
}


public String amayaHttpGet(String url){
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("GET");
conn.connect();
int responseCode = conn.getResponseCode();
InputStream stream ;
if(responseCode == 200){
stream = conn.getInputStream();
}else{
stream = conn.getErrorStream();
}
String res = getStringFromStream(stream);
return res;
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return null;
}
public String amayaHttpPost(String url,HashMap<String,String> params){

try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("POST");
if(params != null){
Iterator<Map.Entry<String,String>> iterator = params.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry<String, String> next = iterator.next();
conn.setRequestProperty(next.getKey(),next.getValue());
}
}
conn.connect();
int responseCode = conn.getResponseCode();
InputStream stream = null;
if(responseCode == 200){
stream = conn.getInputStream();
}else{
stream = conn.getErrorStream();
}
String res = getStringFromStream(stream);
return res;
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return null;
}

private String getStringFromStream(InputStream stream) {
byte[] buf = new byte[2048];
int len ;
StringBuffer sb = new StringBuffer();
try {
while((len=stream.read(buf))!= -1){
sb.append(new String(buf,0,len));
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}

}
13 changes: 13 additions & 0 deletions src/com/iyoudang/matrix/share/util/AmayaShareConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,22 @@ public class AmayaShareConstants {
public static final String AMAYA_RENREN_API_KEY = "18a995f3720043daace0052eb4e18bb5";
public static final String AMAYA_RENREN_SECRET_KEY = "6551cd003e4141b19f46bd0aad002869";
public static final String AMAYA_RENREN_APP_ID = "271466";

/**
* 豆瓣
*/

public static final String AMAYA_DOUBAN_ID = "064da110f8922a1912e8e332e9b67e4b";// douban API Key
public static final String AMAYA_DOUBAN_SECRET = "5ee975cf795ad48b"; // douban Secret
public static final String AMAYA_DOUBAN_REDIRECT_URI = "http://m.100tv.com";
public static final String AMAYA_DOUBAN_TOKEURL = "https://www.douban.com/service/auth2/token";
public static final String AMAYA_DOUBAN_AUTH_URL = "https://www.douban.com/service/auth2/auth";
public static final String AMAYA_DOUBAN_USER_INFO_URL = "http://api.douban.com/v2/user/";

/**
* Activity回调requestCode
*/
public static final int AMAYA_ACTIVITY_RESULT_DOUBAN = 820;
public static final int AMAYA_ACTIVITY_RESULT_TXWEIBO = 821;
public static final int AMAYA_ACTIVITY_RESULT_SINAWEIBO = 32973;
public static final int AMAYA_ACTIVITY_RESULT_QQ = 10100;//com.tencent.connect.common.Constants.REQUEST_API;
Expand Down
14 changes: 14 additions & 0 deletions src/com/iyoudang/matrix/share/util/AmayaTokenKeeper.java
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,18 @@ public static String getTXWeiboToken(Context context) {
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
return pref.getString("AMAYA_TXWEIBO_TOKEN",null);
}
public static void saveDoubanToken(Context context, String token, String expiresIn) {
if (null == context) {
return;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
pref.edit().putString("AMAYA_DOUBAN_TOKEN",token).putString("AMAYA_DOUBAN_EXPIRESIN",expiresIn).commit();
}
public static String getDoubanToken(Context context) {
if (null == context) {
return null;
}
SharedPreferences pref = context.getSharedPreferences(PREFERENCES_NAME, Context.MODE_APPEND);
return pref.getString("AMAYA_DOUBAN_TOKEN",null);
}
}

0 comments on commit f7a6707

Please sign in to comment.