diff --git a/README.md b/README.md index f05473e..4a3bef5 100644 --- a/README.md +++ b/README.md @@ -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 开发说明 @@ -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 注意 使用微信登录分享需要签名打包,并且签名和包名要和微信平台填入的信息一致。 @@ -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 diff --git a/app/src/main/java/com/tsy/socialsample/MainActivity.java b/app/src/main/java/com/tsy/socialsample/MainActivity.java index 92e660c..18d3579 100644 --- a/app/src/main/java/com/tsy/socialsample/MainActivity.java +++ b/app/src/main/java/com/tsy/socialsample/MainActivity.java @@ -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()); } /** diff --git a/build.gradle b/build.gradle index b5f39e1..fcd4311 100644 --- a/build.gradle +++ b/build.gradle @@ -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' } } diff --git a/social_sdk/.gitignore b/social_sdk/.gitignore index 4978360..e4469d4 100644 --- a/social_sdk/.gitignore +++ b/social_sdk/.gitignore @@ -1,2 +1,3 @@ /build .idea +#*.iml diff --git a/social_sdk/social_sdk.iml b/social_sdk/social_sdk.iml deleted file mode 100644 index 52c4d84..0000000 --- a/social_sdk/social_sdk.iml +++ /dev/null @@ -1,135 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/social_sdk/src/main/java/com/tsy/sdk/social/SSOHandler.java b/social_sdk/src/main/java/com/tsy/sdk/social/SSOHandler.java index d278ba1..942d0b7 100644 --- a/social_sdk/src/main/java/com/tsy/sdk/social/SSOHandler.java +++ b/social_sdk/src/main/java/com/tsy/sdk/social/SSOHandler.java @@ -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 分享内容 diff --git a/social_sdk/src/main/java/com/tsy/sdk/social/SocialApi.java b/social_sdk/src/main/java/com/tsy/sdk/social/SocialApi.java index 424e421..2bf8f03 100644 --- a/social_sdk/src/main/java/com/tsy/sdk/social/SocialApi.java +++ b/social_sdk/src/main/java/com/tsy/sdk/social/SocialApi.java @@ -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 diff --git a/social_sdk/src/main/java/com/tsy/sdk/social/sina/SinaWBHandler.java b/social_sdk/src/main/java/com/tsy/sdk/social/sina/SinaWBHandler.java index 3ee5e46..6a3809e 100644 --- a/social_sdk/src/main/java/com/tsy/sdk/social/sina/SinaWBHandler.java +++ b/social_sdk/src/main/java/com/tsy/sdk/social/sina/SinaWBHandler.java @@ -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; diff --git a/social_sdk/src/main/java/com/tsy/sdk/social/weixin/WXHandler.java b/social_sdk/src/main/java/com/tsy/sdk/social/weixin/WXHandler.java index ffe330e..6fad9ae 100644 --- a/social_sdk/src/main/java/com/tsy/sdk/social/weixin/WXHandler.java +++ b/social_sdk/src/main/java/com/tsy/sdk/social/weixin/WXHandler.java @@ -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 = ""; @@ -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; @@ -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"); @@ -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;