How to get Wechat authorization token?
Asked Answered
W

3

6

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
Waldos answered 15/1, 2015 at 9:28 Comment(4)
Can you show us the output of LogCat ?Callum
Did you figure out the answer @Sinigami? I'm trying to do the same thing and have similar issues.Bridgeport
@Bridgeport No, I left this.Waldos
Hey, Facing the same issue. Please help me to get out of this:(Todhunter
B
4

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.

Buehrer answered 20/1, 2016 at 9:13 Comment(0)
O
2

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

Obadias answered 2/2, 2018 at 15:14 Comment(0)
E
-2

onReq and onResp will be called in WXEntryActivity.java within JAVA reflection

Suppose package name io.github.you

You should create a directory named wxapi,then create a WXEntryActivity.java

You get io.github.you.wxapi.WXEntryActivity.java

In AndroidManifest.xml

<activity
    android:name=".wxapi.WXEntryActivity"
    android:exported="true"
    android:label="@string/title_activity_wxentry"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.NoDisplay" > 

In WXEntryActivity.java

public class WXEntryActivity implements IWXAPIEventHandler{

@Override
public void onReq(BaseReq arg0) {
    SendAuth.Resp r = (SendAuth.Resp)resp;
    String code = r.code;

}

@Override
public void onResp(BaseResp arg0) {
    // TODO Auto-generated method stub
    }
}

Good Luck

Exhibitioner answered 17/1, 2015 at 4:11 Comment(1)
Thank for your answer, I did as you say, but problem still there. Possible reason in that: "api.handleIntent(getIntent(), this)" returs false;Waldos

© 2022 - 2024 — McMap. All rights reserved.