Android的 - 改造2 - 身份验证结果 [英] Android - Retrofit 2 - Authenticator Result

查看:237
本文介绍了Android的 - 改造2 - 身份验证结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用改造(2.0.0-β3),但使用一个Authenticator添加令牌时,我似乎无法从同步调用获取数据。我们对后端记录只显示了很多尝试登录,但我不能从身体获取数据实际添加到头部。

I'm trying to use Retrofit (2.0.0-beta3), but when using an Authenticator to add a token, I can't seem to get the data from the synchronous call. Our logging on the back-end just shows a lot of login attempts, but I can't get the data from the body to actually add to the header.

    public static class TokenAuthenticator implements Authenticator {
    @Override
    public Request authenticate(Route route, Response response) throws IOException {
        // Refresh your access_token using a synchronous api request
        UserService userService = createService(UserService.class);

        Call<Session> call = userService.emailLogin(new Credentials("handle", "pass"));

        // This call is made correctly, as it shows up on the back-end.
        Session body = call.execute().body();

        // This line is never hit.
        Logger.d("Session token: " + body.token);

        // Add new header to rejected request and retry it
        return response.request().newBuilder()
                .header("Auth-Token", body.token)
                .build();
        }
    }

我不完全为什么它甚至没有任何东西打印出来太肯定。如何解决这个问题的任何提示将不胜AP preciated,感谢您抽出时间来帮助。

I'm not exactly too sure on why it's not even printing anything out. Any tips on how to solve this issue would be greatly appreciated, thanks for taking the time to help.

这是我一直在阅读有关如何实施改造的来源。

These are the sources I've been reading on how to implement Retrofit.

使用身份验证:


  • <一个href=\"http://stackoverflow.com/a/31624433/3106174\">http://stackoverflow.com/a/31624433/3106174

https://github.com/square/okhttp/wiki /食谱#处理认证

制作同步调用与改造2:

Making synchronous calls with Retrofit 2:

  • https://futurestud.io/blog/retrofit-synchronous-and-asynchronous-requests

推荐答案

我也有类似的认证,并与2.0.0-β2工作。

I have similar authenticator and it works with 2.0.0-beta2.

如果你得到很多从你的登录尝试验证器,我建议要确保当你进行同步调用,您不使用验证器与电话。
这可能在循环结束后,如果您还emailLogin失败。

If you get lots of login attempts from you Authenticator, I suggest make sure that when you make the synchronous call, you are not using Authenticator with that call. That could end up in loop, if also your "emailLogin" fails.

此外,我会建议增加loggingInterceptor看到所有TRAFIC服务器:与改造2 记录

Also I would recommend adding loggingInterceptor to see all trafic to server: Logging with Retrofit 2

这篇关于Android的 - 改造2 - 身份验证结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆