Zendesk SDK JwtAuthentication 不工作

Zendesk SDK JwtAuthentication not working

我有 android 的 SDK,我尝试为聊天和支持模块设置 JWT Identity。

我有一个在文档中定义的 webhook。这行得通。我在日志中看到 zendesk 正在发送我在 JwtIdentity 中给他的用户令牌,它 returns 文档中指定的 jwt,包含名称、电子邮件、jti 和 iat。

webhook 代码:

router.post('/jwt', async (req, res, next) => {
    try {
        console.log(req.body)
        const user_token = req.body.user_token

        const user = await usersService.findByTokend(user_token)

        if (typeof user === "undefined" || user === null) {
            return res.status(404).send('No user found').end()
        }

        const token = jwt.sign({
                name: `${user.lastname} ${user.firstname}`.toLowerCase().trim(),
                email: user.email.toLowerCase().trim(),
            },
            JWT_SECRET,
            {
                expiresIn: '12h',
                jwtid: uuidv4()
            })

        console.log(token)
        return res.status(200).send({
            jwt: token
        }).end()
    } catch (err) {
        console.log(err)
        next(err)
    }
})

初始化期间,我的 webhook 日志和我的 SDK 实现都没有出错。当我调用 activity 并尝试验证失败的用户时。

对于聊天实例,我定义了一个 JwtAuthenticator 如下:

public class ZendeskJwtAuthenticator implements JwtAuthenticator {
    private String user_token;

    public void setUserToken(String user_token) {
        this.user_token = user_token;
    }

    @Override
    public void getToken(JwtCompletion jwtCompletion) {
        try {
            OkHttpClient client = new OkHttpClient();
            
            JSONObject jsonBody = new JSONObject();
            jsonBody.put("user_token", user_token);

            RequestBody requestJsonBody = RequestBody.create(
                    jsonBody.toString(),
                    MediaType.parse("application/json")
            );

            Request postRequest = new Request.Builder()
                    .url(url)
                    .post(requestJsonBody)
                    .build();

            client.newCall(postRequest).enqueue(new Callback() {
                @Override
                public void onFailure(@NotNull Call call, @NotNull IOException e) {
                    jwtCompletion.onError();
                }

                @Override
                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    try {
                        String body = response.body().string();
                        JsonObject jsonObject = JsonParser.parseString(body).getAsJsonObject();
                        if (jsonObject.has("jwt")) {
                            jwtCompletion.onTokenLoaded(jsonObject.get("jwt").getAsString());
                        } else {
                            jwtCompletion.onError();
                        }
                    } catch (Exception e) {
                        jwtCompletion.onError();
                    }
                }
            });
        } catch (Exception exception) {
            jwtCompletion.onError();
        }
    }
}

我调用集合标识如下:

private void setUserIdentity(String user_token) {
    Identity identity = new JwtIdentity(user_token);
    ZendeskJwtAuthenticator autenticator = new ZendeskJwtAuthenticator();
    autenticator.setUserToken(user_token);
    Zendesk.INSTANCE.setIdentity(identity);
    Chat.INSTANCE.setIdentity(autenticator);
}

初始化后调用:

Zendesk.INSTANCE.init(appContext, zendeskUrl, appId, clientId);
Chat.INSTANCE.init(appContext, chatKey, chatAppId);
Support.INSTANCE.init(Zendesk.INSTANCE);
this.setUserIdentity(options);

在我打开聊天时的日志中:

I/okhttp.OkHttpClient: --> POST https://id.zopim.com/authenticated/web/jwt (318-byte body)
I/ReactNativeJNI: Memory warning (pressure level: TRIM_MEMORY_RUNNING_CRITICAL) received by JS VM, running a GC
I/okhttp.OkHttpClient: <-- 400 https://id.zopim.com/authenticated/web/jwt (2412ms, 63-byte body)
W/JwtLoginDetailsProvider: Error fetching authentication token. There may be an issue with your JWT. Chat will proceed unauthenticated: 400

当我打开帮助中心时:

I/okhttp.OkHttpClient: --> GET https://<company>.zendesk.com/hc/api/mobile/fr/article_tree.json?category_ids=&section_ids=&include=categories%2Csections&limit=5&article_labels=&per_page=1000&sort_by=created_at&sort_order=desc
I/okhttp.OkHttpClient: --> POST https://<company>.zendesk.com/access/sdk/jwt (22-byte body)
I/okhttp.OkHttpClient: <-- 403 https://<company>.zendesk.com/access/sdk/jwt (3350ms, unknown-length body)
I/okhttp.OkHttpClient: <-- 400 Response body was null, failed to auth user. https://<company>.zendesk.com/hc/api/mobile/fr/article_tree.json?category_ids=&section_ids=&include=categories%2Csections&limit=5&article_labels=&per_page=1000&sort_by=created_at&sort_order=desc (3359ms, 2-byte body)

我找到了解决方案,post-it 如果有人遇到同样的问题。

事实上,我的配置和工作确实都很好。我唯一的错误是 我使用同一封电子邮件来识别我的应用内客户和我的 zendesk 管理员。

就是这样。祝你有个美好的一天。