OAuth Authentication with Twitter API failed!

2019-05-26 14:20发布

I used R's packages 'twitteR' and 'ROAuth' to access Twitter API. I tried this R code to register my R session:

tw<-OAuthFactory$new(consumerKey="mykeyhere",consumerSecret="mysecrethere",  
requestURL="http://api.twitter.com/oauth/request_token",
accessURL="http://api.twitter.com/oauth/access_token",
authURL="http://api.twitter.com/oauth/authorize")
tw<-handshake()

I used http instead of https because the latter gave me an error of SSL certification fail. Then, I enter a PIN code obtained from a given URL and registered with Twitter:

registerTwitterOAuth(tw)

The result of the above command was:

[1] TRUE

I think that I successfully registered using OAuth. However, when I tried to access protected users' profiles, for example,

getURL("http://api.twitter.com/1/followers/ids.json?cursor=-1&user_id=XXXXXXXX")

I got this:

[1] "{\"error\":\"Not authorized\",\"request\":\"\\/1\\/followers\\/ids.json?cursor=-1&user_id=XXXXXXXX\"}"

I also checked my verification by using:

getURL("http://api.twitter.com/1/account/verify_credentials.json")

And this was the result:

[1] "{\"error\":\"Could not authenticate you.\",\"request\":\"\\/1\\/account\\/verify_credentials.json\"}"

Would you mind helping me please? Thank you very much.

1条回答
孤傲高冷的网名
2楼-- · 2019-05-26 14:46

You should use getUser(...),searchTwitter(...),tw$OAuthRequest(...) and so on instead of directly use getURL(...) by yourself because getURL() does not contain session context (which register using registerTwitterOAuth).

example of tw$OAuthRequest http://goo.gl/6IwdU

查看更多
登录 后发表回答