微信第三方登入Android注意事項
阿新 • • 發佈:2019-02-03
Step 1 申請微信應用
進入:https://open.weixin.qq.com/ 交300軟妹幣,等通過。
填寫幾個重要資訊
1.應用簽名
2.包名
3.應用名字,重要事情說三遍,由於測試的心裡,應用名沒管它.其它都對,一直報ignore wechat app signature validation,吐血了,最後還好發現了,
不一樣試試改成申請的應用名字.終於好了.
Step 2配製檔案與lib
2.1.配製com.test是你APP包名
<activity android:name="com.test.wxapi.WXEntryActivity" android:configChanges="keyboardHidden|orientation|screenSize" android:exported="true" android:label="@string/app_name" android:launchMode="singleTop" android:screenOrientation="portrait" > <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <data android:scheme="sdksample" /> </intent-filter> </activity>
2.2匯入libammsdk包
Step 3呼叫的程式碼
3.1發起請求的程式碼
if(api==null){ api=WXAPIFactory.createWXAPI(WelcomeActivity.this, APP_ID, false); api.registerApp(APP_ID); } if (!api.isWXAppInstalled()) { //提醒使用者沒有按照微信 Toast.makeText(WelcomeActivity.this, "沒有安裝微信,請先安裝微信!", Toast.LENGTH_SHORT).show(); return; } SendAuth.Req req = new SendAuth.Req(); req.scope = "snsapi_userinfo"; req.state = "wechat_sdk_demo_test"; api.sendReq(req);
3.2接收微信APP返回的使用者授權資訊
public class WXEntryActivity extends Activity implements IWXAPIEventHandler{ private IWXAPI api; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.flash_activity); if(api==null){ //wx30a9402cd2a87cf3 api=WXAPIFactory.createWXAPI(this, APP_ID, false); api.registerApp(APP_ID); } api.handleIntent(getIntent(), this); handleIntent(getIntent()); } // 微信傳送請求到第三方應用時,會回撥到該方法 @Override public void onReq(BaseReq req) { switch (req.getType()) { case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX: // goToGetMsg(); break; case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX: // goToShowMsg((ShowMessageFromWX.Req) req); break; default: break; } } // 第三方應用傳送到微信的請求處理後的響應結果,會回撥到該方法 @Override public void onResp(BaseResp resp) { // int result = 0; String result=""; switch (resp.errCode) { case BaseResp.ErrCode.ERR_OK: result = "errcode_success"; break; case BaseResp.ErrCode.ERR_USER_CANCEL: result = "errcode_cancel"; break; case BaseResp.ErrCode.ERR_AUTH_DENIED: result = "errcode_deny"; break; default: result = "errcode_unknown"; break; } Toast.makeText(this, result, Toast.LENGTH_LONG).show(); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); handleIntent(intent); } private void handleIntent(Intent intent) { SendAuth.Resp resp = new SendAuth.Resp(intent.getExtras()); if (resp.errCode == BaseResp.ErrCode.ERR_OK) { //使用者同意 } } }