How to use OAuth at XING with R

2019-05-09 08:01发布

I'm trying to authenticate to the XING API (api.xing.com) using ROAuth (v0.9.2).

library(package="RCurl")
library(package="ROAuth")

site <- "https://api.xing.com"
requestTokenPath <- "/v1/request_token"
accessTokenPath <- "/v1/access_token"
authorizePath <- "/v1/authorize"

consumerKey <- "***********"    # blank key for posting
consumerSecret <- "********"    # blank key for posting

requestURL <- paste(site, requestTokenPath, sep="")
accessURL <- paste(site, accessTokenPath, sep="")
authURL <- paste(site, authorizePath, sep="")

credentials <- OAuthFactory$new(consumerKey=consumerKey,
    consumerSecret=consumerSecret,
    requestURL=requestURL,
    accessURL=accessURL,
    authURL=authURL,
    needsVerifier=TRUE)

credentials$handshake(ssl.verifypeer=FALSE)  # skip ssl verification for testing, this is passed through to RCurl

Output:

Error in credentials$handshake(ssl.verifypeer = FALSE) : 
  Invalid response from site, please check your consumerKey and consumerSecret and try again.

I double checked my keys and the URLs, so I'm pretty sure that's not the cause of the error.

  1. Can anybody tell me what I'm doing wrong?
  2. Is there a way to extract the server requests and responses for error analysis?

Thanks, Chris

标签: r roauth
1条回答
beautiful°
2楼-- · 2019-05-09 08:14

I would recommend using the httr package by Hadley Wickham, as I have found it to be particularly helpful when dealing with APIs. And don't forget to CAREFULLY read all relevant documentation...

it looks like XING uses OAuth v1 so look at relevant httr documentation for that

I'm not sure if you already know this...but it's kinda a two stage process...

first you send your consumer key and secret to XING which will return to you a token.

then with all three of:

1) consumer key, 2) consumer secret and 3) the token just provided

can you access all the API calls that XING have set up...you will probably need XML to interpret the responses efficiently though.

not sure if this will work but something along the lines of:

require(httr)
xing.app <- oauth_app("xing",key="xxxxxxxxxx", secret="xxxxxxxxxxxxxxx")
xing.urls <- oauth_endpoint(NULL, "authorize", "access_token",base_url = "https://api.xing.com/v1/")
xing.token <- oauth1.0_token(xing.urls, xing.app)
xing.token

that token in xing.token is for that unique key, and secret combination, i.e. that user...but once you have it...you don't need to keep requesting it...you can store it in your .Rprofile file or something...and then refer to it as an option in your GET or POST commands.

user.signature <- sign_oauth1.0(xing.app, token = token.string.from.xing.token, token_secret = token.secret.from.xing.token)

# so I think as an example you can have this...
id <- "yyyyyyy"
GET(url= paste0("https://api.xing.com/v1/users/",id), config=user.signature)

hope that helps....there may be a few errors in the code as this isn't tested as I don't have your consumer key or secret. I haven't fully read the documentation but I don't think its too far off...please feel free to comeback with corrections...when you actually test it...

out of curiosity...what are you using the API for?

查看更多
登录 后发表回答