Skip to content

Commit

Permalink
增加SinaWB自定义REDIRCTOR_URL
Browse files Browse the repository at this point in the history
增加WX自定义SCOPE STATE
  • Loading branch information
tsy12321 committed Jan 4, 2017
1 parent ad52bba commit 8695edf
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 206 deletions.
90 changes: 62 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,35 +95,12 @@ public class MainActivity extends AppCompatActivity implements IWeiboHandler.Res

### 2.3 API调用

当第三方授权成功后获取到access_token,会提供一些API查询用户信息,刷新access_token等。现在逐步开始封装API,方便使用
SDK中封装了部分API,比如当第三方授权成功后获取到access_token,会提供一些API查询用户信息,刷新access_token等。

#### 2.3.1 微信API (WXApi)
**!注意:由于调用这些API会用到appsecret,所以一般不建议放到客户端做**

1. getAccessToken(String wxAppId, String wxAppSecret,
String code,
final Callback callback)
具体API见底附录。

```java
/**
* 获取access_token
* @param wxAppId wx appid
* @param wxAppSecret wx appsecret
* @param code 调用微信登录获取的code
* @param callback
*/
```

1. getUserInfo(String openid, String access_token,
final Callback callback)

```java
/**
* 获取用户信息
* @param openid openid
* @param access_token access_token
* @param callback
*/
```

## 3 开发说明

Expand Down Expand Up @@ -298,7 +275,19 @@ PlatformType:

朋友圈:PlatformType.WEIXIN_CIRCLE(用于微信朋友圈分享)

#### 4.1.4 注意

#### 4.1.4 自定义scope state

可以通过接口setScopeState修改 scope 和 state。

默认scope是 "snsapi_userinfo,snsapi_friend,snsapi_message"
默认state是 "none"

```java
WXHandler.setScopeState("your scope", "your state");
```

#### 4.1.5 注意

使用微信登录分享需要签名打包,并且签名和包名要和微信平台填入的信息一致。

Expand Down Expand Up @@ -452,10 +441,55 @@ if (savedInstanceState != null) {
PlatformConfig.setSinaWB(SINA_WB_APPKEY);
```

#### 4.3.4 注意
#### 4.3.4 自定义REDIRECT_URL

可以通过接口setRedirctUrl修改 REDIRECT_URL

默认REDIRECT_URL是 "https://api.weibo.com/oauth2/default.html"

```java
SinaWBHandler.setRedirctUrl("your RedirctUrl");
```

#### 4.3.5 注意

使用新浪登录分享需要签名打包,并且签名和包名要和新浪平台填入的信息一致。

并且微博开放平台的回调地址(REDIRECT_URL)要和代码中的REDIRECT_URL一致

## 附录

### API列表

#### 1 微信API (WXApi)

1. 获取access_token

```java
/**
* 获取access_token
* @param wxAppId wx appid
* @param wxAppSecret wx appsecret
* @param code 调用微信登录获取的code
* @param callback
*/
```
getAccessToken(String wxAppId, String wxAppSecret,
String code,
final Callback callback)

1. 获取用户信息

```java
/**
* 获取用户信息
* @param openid openid
* @param access_token access_token
* @param callback
*/
```
getUserInfo(String openid, String access_token,
final Callback callback)

## About Me
简书地址:http://www.jianshu.com/users/21716b19302d/latest_articles
Expand Down
6 changes: 1 addition & 5 deletions app/src/main/java/com/tsy/socialsample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ protected void onCreate(Bundle savedInstanceState) {
*/
@OnClick(R.id.btnWXLogin)
public void onWXLogin() {
try {
mSocialApi.doOauthVerify(this, PlatformType.WEIXIN , new MyAuthListener());
} catch (Exception e) {
e.printStackTrace();
}
mSocialApi.doOauthVerify(this, PlatformType.WEIXIN , new MyAuthListener());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.2'
classpath 'com.android.tools.build:gradle:2.2.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
}
}
Expand Down
1 change: 1 addition & 0 deletions social_sdk/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/build
.idea
#*.iml
135 changes: 0 additions & 135 deletions social_sdk/social_sdk.iml

This file was deleted.

11 changes: 0 additions & 11 deletions social_sdk/src/main/java/com/tsy/sdk/social/SSOHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ public void authorize(Activity activity, AuthListener authListener) {

}

/**
* 登录授权 供微信使用
* @param activity
* @param scope 微信scope
* @param state 微信state
* @param authListener 授权回调
*/
public void authorize(Activity activity, String scope, String state, AuthListener authListener) {

}

/**
* 分享
* @param shareMedia 分享内容
Expand Down
18 changes: 0 additions & 18 deletions social_sdk/src/main/java/com/tsy/sdk/social/SocialApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,24 +84,6 @@ public void doOauthVerify(Activity activity, PlatformType platformType, AuthList
ssoHandler.authorize(activity, authListener);
}

/**
* 第三方登录授权 供微信使用
* @param activity
* @param platformType 第三方平台
* @param scope 微信scope
* @param state 微信state
* @param authListener 授权回调
*/
public void doOauthVerify(Activity activity, PlatformType platformType,
String scope, String state, AuthListener authListener) throws Exception {
if(platformType != PlatformType.WEIXIN) {
throw new Exception("This doOauthVerify is only for weixin");
}
SSOHandler ssoHandler = getSSOHandler(platformType);
ssoHandler.onCreate(mContext, PlatformConfig.getPlatformConfig(platformType));
ssoHandler.authorize(activity, scope, state, authListener);
}

/**
* 分享
* @param platformType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,17 @@ public class SinaWBHandler extends SSOHandler {
private AuthListener mAuthListener;
private ShareListener mShareListener;

private final String REDIRECT_URL = "http://sns.whalecloud.com/sina2/callback";// 应用的回调页
private static String REDIRECT_URL = "https://api.weibo.com/oauth2/default.html";// 应用的回调页 要和微博开放平台的回调地址一致
private final String SCOPE = "";

/**
* 设置微博 REDIRECT_URL
* @param redirctUrl
*/
public static void setRedirctUrl(String redirctUrl) {
REDIRECT_URL = redirctUrl;
}

@Override
public void onCreate(Context context, PlatformConfig.Platform config) {
this.mContext = context;
Expand Down
23 changes: 16 additions & 7 deletions social_sdk/src/main/java/com/tsy/sdk/social/weixin/WXHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public class WXHandler extends SSOHandler {
private Activity mActivtiy;

private IWXAPI mWXApi;

//默认scope 和 state
private static String sScope = "snsapi_userinfo,snsapi_friend,snsapi_message";
private static String sState = "none";

private IWXAPIEventHandler mEventHandler;
private String mLastTransaction = "";

Expand Down Expand Up @@ -79,6 +83,16 @@ public void onReq(BaseReq req) {
};
}

/**
* 设置scope和state
* @param scope
* @param state
*/
public static void setScopeState(String scope, String state) {
sScope = scope;
sState = state;
}

@Override
public void onCreate(Context context, PlatformConfig.Platform config) {
this.mContext = context;
Expand All @@ -95,11 +109,6 @@ public boolean isInstall() {

@Override
public void authorize(Activity activity, AuthListener authListener) {
authorize(activity, sScope, "none", authListener);
}

@Override
public void authorize(Activity activity, String scope, String state, AuthListener authListener) {
if(!isInstall()) {
authListener.onError(this.mConfig.getName(), "wx not install");
LogUtils.e("wx not install");
Expand All @@ -110,8 +119,8 @@ public void authorize(Activity activity, String scope, String state, AuthListene
this.mAuthListener = authListener;

SendAuth.Req req1 = new SendAuth.Req();
req1.scope = scope;
req1.state = state;
req1.scope = sScope;
req1.state = sState;
req1.transaction = buildTransaction("authorize");
mLastTransaction = req1.transaction;

Expand Down

0 comments on commit 8695edf

Please sign in to comment.