英文:
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传递它?
任何指导都将是有帮助的。
谢谢!
更新:控制台查询结果
英文:
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 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
= "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
);
}
Usage:
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();
}
});
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
答案1
得分: 1
真正的问题是因为网址内部有冒号符号“:”,所以网址应以点和斜杠符号“./”开头:
@POST("./accounts:signInWithPassword")
在 GitHub 上找到了这个帖子,它有所帮助:https://github.com/square/retrofit/issues/2730
更新:我解释一下为什么我使用带有斜杠符号的 "accounts/signInWithPassword"
这样的网址,而不是冒号符号:我最初尝试使用冒号,但出现了“URL 格式错误”的错误,所以我对这个错误进行了更深入的挖掘
英文:
The real issue was because of colon symbol inside url ":", so url should start from dot and slash symbols "./":
@POST("./accounts:signInWithPassword")
Found this on github and it helps https://github.com/square/retrofit/issues/2730
UPD: A little explanation why I used url like "accounts/signInWithPassword"
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
答案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({"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
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论