如何通过代码/API调用或不登录浏览器来为用户生成_forum_session和_t?

Can you back up and describe what problem having those cookies is going to solve?

What are you trying to accomplish? Are you trying to have Discourse authenticate against your app? Maybe look at DiscourseConnect - Official Single-Sign-On for Discourse (sso)?