Android Java: Retrofit2 + Google API使用电子邮件和密码进行身份验证返回404错误。

huangapple go评论62阅读模式
英文:

Android Java: Retrofit2 + google api auth with email and password gives 404

问题

我已经使用Firestore实现了身份验证,它运行良好,现在通过Google API重新实现,但是得到了HTTP状态码"404"和空消息:

D/RESPONSE FIREBASE: Response{protocol=h2, code=404, message=, url=https://identitytoolkit.googleapis.com/v1/accounts/signInWithPassword?key=000080511101}

网络服务:

public class NetworkService {
    private static NetworkService instance;
    private static final String BASE_URL
        = "https://identitytoolkit.googleapis.com/v1/";
    private Retrofit retrofit;

    private NetworkService() {
        retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    }

    public static NetworkService getInstance() {
        if (instance == null) {
            instance = new NetworkService();
        }
        return instance;
    }

    public PlaceHolderApi getJsonApi() {
        return retrofit.create(PlaceHolderApi.class);
    }
}

API:

public interface PlaceHolderApi {

@FormUrlEncoded
@POST("accounts/signInWithPassword")
Call<Transaction.Result> loginWithEmail(
    @Query("key") String key,
    @Field("email") String email,
    @Field("password") String password,
    @Field("returnSecureToken") boolean returnSecureToken
    );
}

用法:

NetworkService.getInstance()
    .getJsonApi().loginWithEmail("000080511101", email, password, true)
    .enqueue(new Callback<Transaction.Result>() {
        @Override
        public void onResponse(Call<Transaction.Result> call, Response<Transaction.Result> response) {
            Log.d("RESPONSE FIREBASE", response.toString());
            Log.d("RESPONSE MESSAGE", response.message());
        }

        @Override
        public void onFailure(Call<Transaction.Result> call, Throwable t) {
            t.printStackTrace();
        }
    });

文档表示我应该使用内容类型application/JSON,但如何在这里使用它或通过retrofit通过http传递它?
任何指导都将是有帮助的。

谢谢!

更新:控制台查询结果

Android Java: Retrofit2 + Google API使用电子邮件和密码进行身份验证返回404错误。

英文:

I've implemented auth with Firestore and it works fine and now redoing it via Google API and get http status "404" and empty message:

D/RESPONSE&#160;FIREBASE: Response{protocol=h2, code=404, message=, url=https://identitytoolkit.googleapis.com/v1/accounts/signInWithPassword?key=000080511101}

Network service:

public class NetworkService {
    private static NetworkService instance;
    private static final String BASE_URL
        = &quot;https://identitytoolkit.googleapis.com/v1/&quot;;
    private Retrofit retrofit;

    private NetworkService() {
        retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    }

    public static NetworkService getInstance() {
        if (instance == null) {
            instance = new NetworkService();
        }
        return instance;
    }

    public PlaceHolderApi getJsonApi() {
        return retrofit.create(PlaceHolderApi.class);
    }
}

Api

public interface PlaceHolderApi {

@FormUrlEncoded
@POST(&quot;accounts/signInWithPassword&quot;)
Call&lt;Transaction.Result&gt; loginWithEmail(
    @Query(&quot;key&quot;) String key,
    @Field(&quot;email&quot;) String email,
    @Field(&quot;password&quot;) String password,
    @Field(&quot;returnSecureToken&quot;) boolean returnSecureToken
    );

}

Usage:

    NetworkService.getInstance()
        .getJsonApi().loginWithEmail(&quot;000080511101&quot;, email, password, true)
        .enqueue(new Callback&lt;Transaction.Result&gt;() {
            @Override
            public void onResponse(Call&lt;Transaction.Result&gt; call, Response&lt;Transaction.Result&gt; response) {
                Log.d(&quot;RESPONSE FIREBASE&quot;, response.toString());
                Log.d(&quot;RESPONSE MESSAGE&quot;, response.message());
            }

            @Override
            public void onFailure(Call&lt;Transaction.Result&gt; call, Throwable t) {
                t.printStackTrace();
            }
        });

Documentation says that I should use Content type application/JSON, but how to use it here or pass it via http using retrofit?
Any directions will be helpful.

Thanks!

UPD: Console query result

Android Java: Retrofit2 + Google API使用电子邮件和密码进行身份验证返回404错误。

答案1

得分: 1

真正的问题是因为网址内部有冒号符号“:”,所以网址应以点和斜杠符号“./”开头:

@POST("./accounts:signInWithPassword")

在 GitHub 上找到了这个帖子,它有所帮助:https://github.com/square/retrofit/issues/2730

更新:我解释一下为什么我使用带有斜杠符号的 &quot;accounts/signInWithPassword&quot; 这样的网址,而不是冒号符号:我最初尝试使用冒号,但出现了“URL 格式错误”的错误,所以我对这个错误进行了更深入的挖掘 Android Java: Retrofit2 + Google API使用电子邮件和密码进行身份验证返回404错误。

英文:

The real issue was because of colon symbol inside url ":", so url should start from dot and slash symbols "./":

    @POST(&quot;./accounts:signInWithPassword&quot;)

Found this on github and it helps https://github.com/square/retrofit/issues/2730

UPD: A little explanation why I used url like &quot;accounts/signInWithPassword&quot; with slash symbol inside instead of colon symbol: I tried with colon first, but got an error "Malformed url" so I dug a bit deeper with that mistake Android Java: Retrofit2 + Google API使用电子邮件和密码进行身份验证返回404错误。

答案2

得分: 0

你可以像这样添加一个头部。但是我认为如果你遗漏了头部响应,错误代码就不会是404。

无论如何,尝试一下。

@FormUrlEncoded
@Headers({"Content-Type: application/json"})
@POST("accounts/signInWithPassword")
Call<Transaction.Result> loginWithEmail(
    @Query("key") String key,
    @Field("email") String email,
    @Field("password") String password,
    @Field("returnSecureToken") boolean returnSecureToken
);
英文:

You can add a header like this. But I think if you miss the header response, the error code wouldn't be 404.

Anyway, try this.

@FormUrlEncoded
@Headers({&quot;Content-Type: application/json&quot;})
@POST(&quot;accounts/signInWithPassword&quot;)
Call&lt;Transaction.Result&gt; loginWithEmail(
    @Query(&quot;key&quot;) String key,
    @Field(&quot;email&quot;) String email,
    @Field(&quot;password&quot;) String password,
    @Field(&quot;returnSecureToken&quot;) boolean returnSecureToken
);

huangapple
  • 本文由 发表于 2020年8月26日 01:31:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63584202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定