How to get Youchat authorization token?

Purpose: to get the token that I need to send to the application server

Problem: registered returns true, executed queries return true, but onReq and onResp not 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); Le(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); Le(this, "request done: " + requestDone); SendAuth.Resp resp = new SendAuth.Resp(); requestDone = api.sendResp(resp); Le(this, "request done: " + requestDone); } @Override public void onReq(BaseReq baseReq) { Le(this, "scope: " + ((SendAuth.Req) baseReq).scope); } @Override public void onResp(BaseResp baseResp) { Le(this, "token: " + ((SendAuth.Resp) baseResp).token); } } 

Log 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 
+6
source share
3 answers

I ran into the same problem and decided in two steps.

First, check if you have successfully switched to the wechat application and are authorized. If not, check if you are using the same signature key that you signed for wechat. (for example, if you signed with the release key and compiled using the debug key, the wechat application will not open)

Secondly, according to the wechat document , the class name should be WXEntryActivity and should be placed in a package named wxapi under the package with the name that you registered on 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 class name is necessary, but I'm sure you can call sendReq in another class and handle the response using WXEntryActivity.

Hope this is helpful.

+4
source

had the same problem! Edwards answered a lot.

WxEntryActivity should be in the package with the name that you registered with wechat!

Especially if you have several build options (debugging, release): Wechat login - do not receive tokens

+1
source

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

Assume the package name is io.github.you

You should create a directory called wxapi and then create 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 } } 

Luck

-2
source

Source: https://habr.com/ru/post/981039/


All Articles