사용자의 Reddit 프론트 페이지를 얻으려고합니다. Token Retrieval (code flow)을 통해 인증 토큰을 성공적으로 받았습니다. Postman을 통해 예상 된 JSON 응답을 얻을 수 있었지만 Retrofit을 사용하여 동일한 결과를 얻을 수는 없습니다. 콜백에서 onFailure()가 트리거되어 요청이 시간 초과 된 것처럼 보입니다. scope, identity, mysubreddits 및 read를 사용하고 있습니다.Reddit Api GET 요청이 우편 배달부에서 성공했지만 Retrofit으로 실패합니다.
추가 참고 : 범위가 충분하지 않고 만료 된 인증 토큰을 사용할 때 아래 코드로 401 및 403 응답을 받았습니다.
관련 상수 :
redditToken = (actual auth token String)
RedditConstants.REDDIT_BASE_URL_OAUTH2 = "https://oauth.reddit.com"
관련 방법 섹션 :
if (redditToken != null) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(RedditConstants.REDDIT_BASE_URL_OAUTH2)
.addConverterFactory(GsonConverterFactory.create())
.build();
Api api = retrofit.create(Api.class);
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "bearer " + redditToken);
headers.put("User-Agent", RedditConstants.REDDIT_USER_AGENT);
Call<RedditFeed> call = api.getFeed(headers);
call.enqueue(new Callback<RedditFeed>() {
@Override
public void onResponse(Call<RedditFeed> call, Response<RedditFeed> response) {
Log.d("FINDME", "response "+ response.toString());
if (response.isSuccessful()) {
Log.d("FINDME", "response was a success! we got the feed!");
} else {
Log.d("FINDME", "responce was not successfull triggered");
}
}
@Override
public void onFailure(Call<RedditFeed> call, Throwable t) {
Log.d("FINDME", "onFailure called from populateRedditFeed");
}
});
} else {
Toast.makeText(this, "Please Login with Reddit", Toast.LENGTH_SHORT).show();
}
개조 인터페이스 :
public interface Api {
@GET(".")
Call<RedditFeed> getFeed (
@HeaderMap Map<String, String> headers
);
}
로그 결과 :
D/NetworkSecurityConfig: No Network Security Config specified, using
platform default
I/zygote: Do full code cache collection, code=123KB, data=105KB
After code cache collection, code=111KB, data=79KB
D/FINDME: onFailure called from populateRedditFeed
이죠 성공 :
onFailure에 Throwable을 인쇄하면 오류의 원인을 알 수 있습니다. 우리가 도울 수 있도록 여기에 게시하십시오 – Kushan
코드를 수정하지 않아도됩니다. 나는 지금 성공적인 응답을 얻고있다. 전에 내가 스타 벅스에 있었고 지금은 집에 WiFi를 사용하고 있습니다. 아마 그 일은 그 일과 관련이 있습니다. 내일 스타 벅스에서 다시 테스트 해 보겠습니다. –
하하 cool :) 알아두면 좋겠어. 코드가 괜찮아서 401과 403 코드를 지적 했으므로 인증 토큰에 대해 알고 있다는 것을 알았습니다 ... 그래서 내가 던지기 가능 여부에 대해 물었습니다 로그, 어쨌든 로그를 유지하면 도움이 될 것입니다. – Kushan