2017-09-11 4 views
0

タイトルには、私はreddit APIに接続しようとしています。プロフィール(コメント抽出ツール)にアプリを作成してコピーしましたパブリックキーと秘密キーを貼り付け、リダイレクトURIとURLについてはhttp://localhost:1410/を使用します。アプリはスクリプトですが、同じ結果でウェブアプリも試しました。htthパッケージのoauth_token2.0を使用して、常に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") 
) 

Webブラウザが開き、私は私が何をするかが不明だったトークンを許可または拒否するように求め、そしてすべてがうまくようだが、それは常にこのメッセージ

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_ のアクセス _token関数では、 "use_basic_auth"のデフォルトはFALSEに設定されています。

トークン関数がacces_token関数を呼び出すように見えるので、私はそのデフォルトをどのように変更するのか考えていませんが、私の洞察が私たちに一歩近づくことを望みます(最終的な解決策を見つけたら、これを私と共有していますか?)

+0

はここに更新します私が考えていることはかなり疲れました.Hadleyや他の誰かが何をする必要があるかというアイデアがあることを望んで、httrパッケージgithubに関する問題を開いたことがあります。私は間違っている)。 –

0

私は私たちの問題を解決しました! htth githubページの#485の引き出し要求は、init.oauth2.0()がuse_basic_authを渡すようにすることで、私たちの問題を解決しました。 ですから、次の行追加することによって、彼のHTTRのバージョンをインストールすることができます。ここでは

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

を私のために働いた私の完全なコードは、(鍵と秘密定義するフォレない操作を行う)である。

#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") 
関連する問題