Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Wechat authorization token?

Tags:

android

wechat

Target: get token which I need to send to the app server

Problem: registered returns true, requests done returns true, but onReq and onRespdid not get called. Here is the code:

public class WeChatActivity extends Activity implements IWXAPIEventHandler {

    private static final String APP_ID = ;
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signin);

        api = WXAPIFactory.createWXAPI(this, APP_ID, true);
        api.handleIntent(getIntent(), this);

        regToWx();
        getAuthToken();
    }

    private void regToWx() {

        api.handleIntent(getIntent(), this);
        boolean registered = api.registerApp(APP_ID);

        L.e(this, "registered: " + registered);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        setIntent(intent);
        api.handleIntent(intent, this);
    }

    public void getAuthToken() {
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "post_timeline";
        req.state = "none";

        boolean requestDone = api.sendReq(req);
        L.e(this, "request done: " + requestDone);

        SendAuth.Resp resp = new SendAuth.Resp();
        requestDone = api.sendResp(resp);
        L.e(this, "request done: " + requestDone);
    }

    @Override
    public void onReq(BaseReq baseReq) {
        L.e(this, "scope: " + ((SendAuth.Req) baseReq).scope);
    }

    @Override
    public void onResp(BaseResp baseResp) {
        L.e(this, "token: " + ((SendAuth.Resp) baseResp).token);
    }
}

Log cat output:

D/MicroMsg.SDK.WXApiImplV10﹕ check signature:308202eb30820254a003020...
D/MicroMsg.SDK.WXApiImplV10﹕ pass
D/MicroMsg.SDK.WXApiImplV10﹕ register app cn.wegazine.wegazine
D/MicroMsg.SDK.MMessage﹕ send mm message, intent=Intent { act=com.tencent.mm.plugin.openapi.Intent.ACTION_HANDLE_APP_REGISTER (has extras) }, perm=com.tencent.mm.permission.MM_MESSAGE
E/WeChatActivity﹕ registered: true
D/MicroMsg.SDK.WXApiImplV10﹕ check signature:308202eb30820...
D/MicroMsg.SDK.WXApiImplV10﹕ pass
D/MicroMsg.SDK.MMessageAct﹕ send mm message, intent=Intent { flg=0x18000000 cmp=com.tencent.mm/.plugin.base.stub.WXEntryActivity (has extras) }
E/WeChatActivity﹕ request done: true
D/MicroMsg.SDK.WXApiImplV10﹕ check signature:308202eb30820...
D/MicroMsg.SDK.WXApiImplV10﹕ pass
D/MicroMsg.SDK.MMessageAct﹕ send mm message, intent=Intent { flg=0x18000000 cmp=com.tencent.mm/.plugin.base.stub.WXEntryActivity (has extras) }
E/WeChatActivity﹕ request done: true
like image 242
Sinigami Avatar asked Jan 15 '15 09:01

Sinigami


People also ask

What is WeChat Open ID?

OpenId is an id of a Wechat personal account when it is following an Wechat Service Account. And the OpenIds are different for the same Wechat personal account in different Wechat Service Accounts.


2 Answers

I've face the same problem and solved with two steps.

First check if you've successfully jumped to the wechat app and authorized. If not, check if you're using the same signing key that you signed to wechat. (ex. if you signed with the release key and compile with debug key, then wechat app won't open)

Second, by wechat document, the class name should be WXEntryActivity and should be put under a package named wxapi under the package with the name you registered at wechat.

Example in the document: If you register with "net.sourceforge.simcpux", the project structure should look like this

Image

Also, add api.HandleIntent(getIntent(), this) after sendReq and sendResp

Not sure if the classname is neccessary, but I'm sure you can call sendReq in other class and process response with WXEntryActivity

Hope this is helpful.

like image 60
Edward Avatar answered Oct 06 '22 00:10

Edward


had the same issue! Edwards answer helped a lot.

WxEntryActivity needs to be in the package with the name you registered at wechat!

Especially when you have multiple build variants (debug, release): Wechat login - do not receive token

like image 22
Maxi Avatar answered Oct 06 '22 01:10

Maxi