2017-09-11 4 views
0

제목에서 알 수 있듯이 reddit API에 연결하려고합니다. 프로필에 comment extractor라는 앱을 만들었고 copy 공개 키와 비밀 키를 붙여 넣은 다음 http://localhost:1410/을 리디렉션 URI 및 URL로 사용하십시오. 응용 프로그램은 스크립트지만, 나는 같은 결과와 함께 웹 애플 리케이션을 시도.htth 패키지의 oauth_token2.0을 사용하여 reddit API에서 항상 401 오류가 발생합니다.

은 내가 사용하고 코드는 단지 Hadleys HTTR을 데모에서 붙여 넣기,하지만 난 내 자신의 (HTTR의 최신 버전, 1.3.1 함께 할 모든 것을)의 키를 교환 복사됩니다.

library(httr) 

# 1. Find OAuth settings for reddit: 
# https://github.com/reddit/reddit/wiki/OAuth2 
reddit <- oauth_endpoint(
    authorize = "https://www.reddit.com/api/v1/authorize", 
    access = "https://www.reddit.com/api/v1/access_token" 
) 

# 2. Register an application at https://www.reddit.com/prefs/apps 
app <- oauth_app("comment extractor", "rrG5wfgHkm5Kvw", "[secret key]") 


# 3b. If get 429 too many requests, the default user_agent is overloaded. 
# If you have an application on Reddit then you can pass that using: 
token <- oauth2.0_token(
    reddit, app, 
    scope = c("read", "modposts"), 
    use_basic_auth = TRUE, 
    config_init = user_agent("reddit_username") 
) 

웹 브라우저는 내가 허용하거나 토큰을 거부하도록 요청하고있어, 열고, 다 잘 보이지만, 항상이 메시지

Waiting for authentication in browser... 
Press Esc/Ctrl + C to abort 
Authentication complete. 
Error in oauth2.0_access_token(endpoint, app, code = code, user_params = 
user_params, : 
    Unauthorized (HTTP 401). Failed to get an access token. 

내가 함께 무엇을 해야할지 확신했다 실패 사용자 에이전트, 나는 개발자의 이름을 묻는 것으로 나타났습니다. 그래서 임의의 텍스트를 두 개 시도하고, reddit 사용자 이름을 사용했는데, 항상 잘못된 키를 의미하는 401 오류가 나타납니다.

어떤 도움을 크게 감상 할 수

, 내가 가장 기본적인 단계에서 중지 얻고 다음에 무엇을 모를 때 손실에 다소 해요.

답변

0
나는 똑같은 문제에 봉착

! 아직 해결책을 찾지 못했지만 문제가있는 곳을 찾았습니다.

오류는 oauth2.0_ 액세스 _token 기능에서 발생하며 oauth2.0_token 기능에서는 발생하지 않습니다. oauth2.0_token 함수에서 "use_basic_auth"를 TRUE로 설정했습니다. 그러나 oauth2.0_에 는 FALSE로 설정 "use_basic_auth"에 대한 기본 기능 _token을 액세서.

토큰 함수가 acces_token 함수를 호출하는 것으로 보이기 때문에이 기본값을 변경하는 방법을 알지 못합니다.하지만 내 통찰력으로 한발 더 가까이 나아갈 수 있기를 바랍니다 (최종 해결책을 찾았 으면 좋겠습니까? 나와 함께이 공유?) 나는 우리의 문제에 대한 해결책을 발견

+0

여기에 업데이트됩니다 내가 알아낼 수 있다면, 나는 꽤 많이 생각해 낸 것을 다 써 버렸다. 나는 해들리 나 다른 누군가가해야 할 일에 대한 아이디어를 가지고 있기를 바란다. httr 패키지 github에서 문제를 열었다. 잘못하고있는 것). –

0

! HTTR의 GitHub의 페이지 풀 요청 #485가 init.oauth2.0하여 우리의 문제를 해결했다() use_basic_auth를 전달합니다. 그래서 당신은 다음과 같은 라인을 추가하여 자신의 HTTR 버전을 설치할 수 있습니다 여기에

#install.packages("devtools") 
library(devtools) 
devtools::install_github("r-lib/httr#485") 
library(httr) 

을 나를 위해 일한 내 전체 코드입니다 (키와 비밀 정의하지 FORET을) :

#install.packages("httr") 
#install.packages("devtools") 
library(devtools) 
devtools::install_github("r-lib/httr#485") 
library(httr) 

# 1. Find OAuth settings for reddit: 
# https://github.com/reddit/reddit/wiki/OAuth2 
endpoint <- oauth_endpoint(
    authorize = "https://www.reddit.com/api/v1/authorize", 
    access = "https://www.reddit.com/api/v1/access_token" 
) 

# 2. Register an application at https://www.reddit.com/prefs/apps 
app <- oauth_app("Test_App", key, secret) 

# 3b. If get 429 too many requests, the default user_agent is overloaded. 
# If you have an application on Reddit then you can pass that using: 
token <- oauth2.0_token(
    endpoint = endpoint, 
    app=app, 
    scope = c("read", "modposts"), 
    use_basic_auth = TRUE, 
# cache = F, 
    config_init = user_agent("Testing Oauth with httr") 
) 

#trying to make a call 
#Important! Make sure to put oauth.reddit.com instad of reddit.com! 
request_url <- "https://oauth.reddit.com/r/AskReddit/comments/7az0np/what_is_the_most_pointless_piece_of_information/.json" 
response <- GET(request_url, 
       user_agent("Testing Oauth with httr"), 
       config(token = token) 
       ) 

content(response, "text")