3

I'm trying to authenticate the Brightcove Analytics API (OAuth 2.0) with R. My first attempt was using oauth functions in the httr package, and I tried to follow these steps

1) Create a variable for my app using a public and secret key. Done:

library("httr")
myapp <- oauth_app("MyBrightcoveApp", key="mykeyhere", secret = "mysecrethere")

2) Find OAuth settings for Brightcove. The oauth_endpoint() function needs an access URL, which I found "https://oauth.brightcove.com/v3/access_token", and an authorization URL, which I haven't found. I'm not sure Brightcove allows in-browser account authentication.

My next attempt was to use the httr::POST() function. I looked at Brightcove's sample node.js code:

var request = require('request');
var client_id = "{your_client_id}";
var client_secret = "{your_client_secret}";
var auth_string = new Buffer(client_id + ":" + client_secret).toString('base64');
console.log(auth_string);
request({
method: 'POST',
url: 'https://oauth.brightcove.com/v3/access_token',
headers: {
    'Authorization': 'Basic ' + auth_string,
    'Content-Type': 'application/x-www-form-urlencoded'
},
body: 'grant_type=client_credentials'
}, function (error, response, body) {
console.log('Status: ', response.statusCode);
console.log('Headers: ', JSON.stringify(response.headers));
console.log('Response: ', body);
console.log('Error: ', error);
});

which I am trying to translate into R. I started with:

library(httr)
client_id <- "myClientIDhere"
client_secret <- "mySecretHere"
auth_string <- paste0(client_id, ":", client_secret)

but I can't seem to get the necessary data into my POST request.

myresponse <- POST(myrequest, username=auth_string, httpheader='Content-type: application/x-www-form-urlencoded', body = 'grant_type=client_credentials'  )

shows myresponse$request$auth_token as NULL. So does

myresponse <- POST(myrequest, authenticate(client_id, client_secret), httpheader='Content-type: application/x-www-form-urlencoded', body = 'grant_type=client_credentials'  )

Any idea what I might be missing?

Sharon
  • 3,676
  • 3
  • 23
  • 20
  • Quick glance: You need to base64 encode your auth_string. See the base64enc package for that. – Thomas Feb 17 '16 at 22:02
  • Thanks, I did also try auth_string <- RCurl::base64Encode(paste0(client_id, ":",client_secret)) with no luck – Sharon Feb 18 '16 at 00:28

1 Answers1

4

This is a very weird authentication system, and doesn't resemble OAuth very much. The key seems to be using regular authentication to get the access token:

library(httr)

id <- "ee6fb53d-6e0d-40f4-84f9-dc043f6f3399"
secret <- "a33sgCuU_WLFm89oBcgl0FCpdLZhtsbHIunNLJWVBwiir5MCGPinHoORvSw4YnwjURZuZa2b-NGFBNqUvevv3w"

r <- POST("https://oauth.brightcove.com/v3/access_token", 
  body = list(grant_type = "client_credentials"),
  authenticate(id, secret),
  encode = "form"
)
stop_for_status(r)
token <- content(r)$access_token

(That id and secret are for my free 30-day account with nothing in it, and should allow you to verify that the code works.)

hadley
  • 102,019
  • 32
  • 183
  • 245