authenticating into Linkedin API from R

Viewed 446

I would like to be able to extract the Posts from (for example) this page: https://www.linkedin.com/company/google/ from R. In order to do so, I need to authenticate, which is giving me problems. This question asks about authentication here but in this question I try to push a bit further:

  • I have created an application in Linkedin, linked it to a company, and authorized it.
  • I have given the application permission to "Share on LinkedIn" and to "Sign In with LinkedIn"

Now all I have to do is pass my customer ID and secret to the app in order to get my token. I'm using httr to do so. I am passing the credentials to the body of the POST request as follows:

library(httr)
library(tidyverse)

cid <- "REDACTED"
cs <- "REDACTED"

link <- "https://www.linkedin.com/oauth/v2/accessToken"
stuff <- list(grant_type = "client_credentials", client_id = cid, client_secret = cs)

httr::POST(url = link,
           body =  jsonlite::toJSON(stuff, pretty = T, auto_unbox = T),
           encode = "form") %>% 
  content

but I'm receiving the response:

$error
[1] "invalid_request"

$error_description
[1] "A required parameter \"grant_type\" is missing"

So clearly, the grant_type isn't being seen by linkedin even if it's in the body. I've also tried to pass just the grant_type via add_headers, or authenticate, but nothing I do gets the grant_type seen.

I've also tried to pass this

paste0("grant_type=client_credentials&client_id=",cid,"&client_secret=",cs,"")` 

via the body, or via add_headers, or httr::authenticate, to no avail.

Explicitly specifying content_type("application/x-www-form-urlencoded")) also doesn't do anything.

I tried to use httr::verbose(ssl=TRUE) to see what the server was saying and this is what came back:

-> POST /oauth/v2/accessToken HTTP/1.1
-> Host: www.linkedin.com
-> User-Agent: libcurl/7.64.1 r-curl/4.3 httr/1.4.2
-> Accept-Encoding: deflate, gzip
-> Cookie: JSESSIONID=ajax:REDACTED; bscookie="v=REDACTED"; bcookie="v=2&562404ab-585e-4d92-83e2-d373e58ae7e6"; lissc=1; lidc="REDACTED:v=1:sig=REDACTED"; lang=v=2&lang=en-us
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Length: 112
-> 
>> {
>>   "grant_type": "client_credentials",
>>   "client_id": "REDACTED",
>>   "client_secret": "REDACTED"
>> }

<- HTTP/1.1 400 Bad Request
<- Cache-Control: no-cache, no-store
<- Pragma: no-cache
<- Content-Length: 107
<- Content-Type: application/json
<- Content-Encoding: gzip
<- Expires: Thu, 01 Jan 1970 00:00:00 GMT
<- Vary: Accept-Encoding
<- X-FS-UUID: REDACTED
<- Expect-CT: max-age=86400, report-uri="https://www.linkedin.com/platform-telemetry/ct"
<- X-XSS-Protection: 1; mode=block
<- Content-Security-Policy: default-src *; connect-src 'self' https://media-src.linkedin.com/media/ www.linkedin.com s.c.lnkd.licdn.com m.c.lnkd.licdn.com s.c.exp1.licdn.com s.c.exp2.licdn.com m.c.exp1.licdn.com m.c.exp2.licdn.com wss://*.linkedin.com dms.licdn.com https://dpm.demdex.net/id https://lnkd.demdex.net/event blob: https://accounts.google.com/gsi/status https://linkedin.sc.omtrdc.net/b/ss/ www.google-analytics.com static.licdn.com static-exp1.licdn.com static-exp2.licdn.com static-exp3.licdn.com media.licdn.com media-exp1.licdn.com media-exp2.licdn.com media-exp3.licdn.com; img-src data: blob: *; font-src data: *; style-src 'unsafe-inline' 'self' static-src.linkedin.com *.licdn.com; script-src 'report-sample' 'unsafe-inline' 'unsafe-eval' 'self' spdy.linkedin.com static-src.linkedin.com *.ads.linkedin.com *.licdn.com static.chartbeat.com www.google-analytics.com ssl.google-analytics.com bcvipva02.rightnowtech.com www.bizographics.com sjs.bizographics.com js.bizographics.com d.la4-c1-was.salesforceliveagent.com slideshare.www.linkedin.com https://snap.licdn.com/li.lms-analytics/ platform.linkedin.com platform-akam.linkedin.com platform-ecst.linkedin.com platform-azur.linkedin.com; object-src 'none'; media-src blob: *; child-src blob: lnkd-communities: voyager: *; frame-ancestors 'self'
<- X-Frame-Options: sameorigin
<- X-Content-Type-Options: nosniff
<- Strict-Transport-Security: max-age=2592000
<- X-Li-Fabric: prod-ltx1
<- X-Li-Pop: afd-prod-eda6
<- X-Li-Proto: http/1.1
<- X-LI-UUID: lJK+jFRoPBZQB2AhTCsAAA==
<- X-MSEdge-Ref: Ref A: REDACTED Ref B: REDACTED Ref C: 2020-10-09T19:04:39Z
<- Date: Fri, 09 Oct 2020 19:04:38 GMT

What could be the problem? My session:

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252 
[2] LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
 [1] forcats_0.5.0   stringr_1.4.0   dplyr_1.0.2     purrr_0.3.4    
 [5] readr_1.3.1     tidyr_1.1.1     tibble_3.0.3    ggplot2_3.3.2  
 [9] tidyverse_1.3.0 httr_1.4.2     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5       cellranger_1.1.0 pillar_1.4.6     compiler_4.0.2  
 [5] dbplyr_1.4.4     tools_4.0.2      digest_0.6.25    lubridate_1.7.9 
 [9] jsonlite_1.7.1   evaluate_0.14    lifecycle_0.2.0  gtable_0.3.0    
[13] pkgconfig_2.0.3  rlang_0.4.7      reprex_0.3.0     cli_2.0.2       
[17] DBI_1.1.0        rstudioapi_0.11  curl_4.3         yaml_2.2.1      
[21] haven_2.3.1      xfun_0.18        withr_2.3.0      xml2_1.3.2      
[25] knitr_1.29       fs_1.4.2         hms_0.5.3        generics_0.0.2  
[29] vctrs_0.3.4      grid_4.0.2       tidyselect_1.1.0 glue_1.4.2      
[33] R6_2.4.1         fansi_0.4.1      readxl_1.3.1     rmarkdown_2.3   
[37] modelr_0.1.8     blob_1.2.1       magrittr_1.5     backports_1.1.10
[41] scales_1.1.1     ellipsis_0.3.1   htmltools_0.5.0  rvest_0.3.6     
[45] assertthat_0.2.1 colorspace_1.4-1 stringi_1.4.6    munsell_0.5.0   
[49] broom_0.7.0      crayon_1.3.4
0 Answers
Related