Android Java: Retrofit2 + google api 使用电子邮件和密码进行身份验证给出 404

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,但如何在这里使用它或使用改装通过 http 传递它? 任何指示都会有所帮助。

谢谢!

更新:控制台查询结果

您可以像这样添加 header。但我认为如果您错过了 header 响应,错误代码就不会是 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
);

真正的问题是因为 url“:”中的冒号符号,所以 url 应该从点和斜线符号“./”开始:

    @POST("./accounts:signInWithPassword")

在 github 上找到它,它对 https://github.com/square/retrofit/issues/2730

有帮助

UPD:一点解释为什么我使用 url 就像 "accounts/signInWithPassword" 里面有斜杠符号而不是冒号符号:我先尝试使用冒号,但出现错误“格式错误 url “所以我对那个错误进行了更深入的挖掘:)