Skip to content

Commit

Permalink
Use req_retry() (#35)
Browse files Browse the repository at this point in the history
* Hack to avoid "didn't wait long enough after refresh" issue.

* Use req_retry().

I need to get in the habit of playing with this more!
  • Loading branch information
jonthegeek authored Aug 14, 2023
1 parent 7c20a69 commit 7c6b383
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
14 changes: 4 additions & 10 deletions renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,9 @@
},
"httr2": {
"Package": "httr2",
"Version": "0.2.3.9000",
"Source": "GitHub",
"Remotes": "jeroen/curl",
"RemoteType": "github",
"RemoteHost": "api.github.com",
"RemoteRepo": "httr2",
"RemoteUsername": "r-lib",
"RemoteRef": "HEAD",
"RemoteSha": "10daf9f2faad31415ced75f1a0eea2ead96844d2",
"Version": "0.2.3",
"Source": "Repository",
"Repository": "CRAN",
"Requirements": [
"R",
"R6",
Expand All @@ -343,7 +337,7 @@
"rlang",
"withr"
],
"Hash": "6fc6565f9cd29140e3c7be517dfdd815"
"Hash": "193bb297368afbbb42dc85784a46b36e"
},
"ini": {
"Package": "ini",
Expand Down
17 changes: 12 additions & 5 deletions runner-li.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,15 @@ li_client <- httr2::oauth_client(
id = Sys.getenv("LI_CLIENT_ID"),
token_url = "https://www.linkedin.com/oauth/v2/accessToken",
secret = Sys.getenv("LI_CLIENT_SECRET"),
auth = "header",
# Force it to not use cache no matter what else it thinks I want.
name = paste(sample(letters, 10, replace = TRUE), collapse = "")
auth = "header"
)

li_base <- httr2::request("https://api.linkedin.com/rest") |>
httr2::req_oauth_refresh(
client = li_client,
refresh_token = Sys.getenv("LI_REFRESH_TOKEN"),
scope = "r_basicprofile,r_emailaddress,r_liteprofile,r_organization_social,w_member_social,w_organization_social"
) |>
) |>
httr2::req_headers(
`Linkedin-Version` = "202306"
)
Expand Down Expand Up @@ -80,7 +78,16 @@ li_post <- li_base |>
)
)

posted <- li_post |>
posted <- li_post |>
# Sometimes it fails the first time or three. Still trying to figure out
# *why*, but this seems to fix it.
httr2::req_retry(
# It fails for lack of auth. I think their server is catching up with the
# refresh usage, maybe?
is_transient = \(x) httr2::resp_status(x) == 401,
max_tries = 10,
backoff = ~ 3
) |>
httr2::req_perform()

if (httr2::resp_status(posted) != 201) {
Expand Down

0 comments on commit 7c6b383

Please sign in to comment.