StevenMMortimer / salesforcer

This R package connects the Salesforce APIs from R using tidy principles.
https://stevenmmortimer.github.io/salesforcer/
Other
81 stars 19 forks source link

Make it easier to get access token #97

Closed carlganz closed 3 years ago

carlganz commented 3 years ago

Issue submission checklist

When filing your issue please make an attempt to troubleshoot a little bit on your own. If your issue is specifically related to a query, but please consider using the "Query issue template". Also, please consider some of the suggestions below before submitting an issue. Thank you!


Issue description

I am using R to interact with some custom RestResources I've developed, and would like to extract the access token from my existing salesforcer session.

The sf_access_token() function sounds like what I want but returns null and documentation seems to indicate I shouldn't use this.

The docs for sf_auth here seem to indicate there is some way to access the token but the salesforcer_state() object in the example isn't defined in the example so I don't know how to recreate myself

reprex

sf_auth()
sf_access_token()
# returns null

Session Info

─ Session info ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 setting  value                       
 version  R version 4.0.5 (2021-03-31)
 os       macOS Big Sur 10.16         
 system   x86_64, darwin17.0          
 ui       RStudio                     
 language (EN)                        
 collate  en_US.UTF-8                 
 ctype    en_US.UTF-8                 
 tz       America/Los_Angeles         
 date     2021-04-26                  

─ Packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 ! package         * version    date       lib source                              
   anytime           0.3.9      2020-08-27 [1] CRAN (R 4.0.2)                      
   askpass           1.1        2019-01-13 [1] CRAN (R 4.0.0)                      
   assertthat        0.2.1      2019-03-21 [1] CRAN (R 4.0.0)                      
   backports         1.2.1      2020-12-09 [1] CRAN (R 4.0.2)                      
   base64enc         0.1-3      2015-07-28 [1] CRAN (R 4.0.0)                      
   broom             0.7.6      2021-04-05 [1] CRAN (R 4.0.2)                      
   cachem            1.0.4      2021-02-13 [1] CRAN (R 4.0.2)                      
   callr             3.7.0      2021-04-20 [1] CRAN (R 4.0.5)                      
   cellranger        1.1.0      2016-07-27 [1] CRAN (R 4.0.0)                      
   checkmate         2.0.0      2020-02-06 [1] CRAN (R 4.0.0)                      
 V cli               2.4.0      2021-04-26 [1] CRAN (R 4.0.5)                      
   clipr             0.7.1      2020-10-08 [1] CRAN (R 4.0.2)                      
   clisymbols        1.2.0      2017-05-21 [1] CRAN (R 4.0.0)                      
   codetools         0.2-18     2020-11-04 [1] CRAN (R 4.0.5)                      
   colorspace        2.0-0      2020-11-11 [1] CRAN (R 4.0.2)                      
   crayon            1.4.1      2021-02-08 [1] CRAN (R 4.0.2)                      
   crosstalk         1.1.1      2021-01-12 [1] CRAN (R 4.0.2)                      
   curl              4.3        2019-12-02 [1] CRAN (R 4.0.0)                      
   data.table        1.14.0     2021-02-21 [1] CRAN (R 4.0.2)                      
   DBI               1.1.1      2021-01-15 [1] CRAN (R 4.0.2)                      
   dbplyr            2.1.1      2021-04-06 [1] CRAN (R 4.0.5)                      
   desc              1.3.0      2021-03-05 [1] CRAN (R 4.0.2)                      
   devtools          2.4.0      2021-04-07 [1] CRAN (R 4.0.5)                      
   digest            0.6.27     2020-10-24 [1] CRAN (R 4.0.2)                      
   dplyr           * 1.0.5      2021-03-05 [1] CRAN (R 4.0.2)                      
   DT                0.18       2021-04-14 [1] CRAN (R 4.0.5)                      
   dygraphs          1.1.1.6    2018-07-11 [1] CRAN (R 4.0.0)                      
   ellipsis          0.3.1      2020-05-15 [1] CRAN (R 4.0.0)                      
   fansi             0.4.2      2021-01-15 [1] CRAN (R 4.0.2)                      
   fastmap           1.1.0      2021-01-25 [1] CRAN (R 4.0.2)                      
   forcats         * 0.5.1      2021-01-27 [1] CRAN (R 4.0.2)                      
   fs                1.5.0      2020-07-31 [1] CRAN (R 4.0.2)                      
   furrr           * 0.2.2      2021-01-29 [1] CRAN (R 4.0.2)                      
   future          * 1.21.0     2020-12-10 [1] CRAN (R 4.0.2)                      
   generics          0.1.0      2020-10-31 [1] CRAN (R 4.0.2)                      
   ggplot2         * 3.3.3      2020-12-30 [1] CRAN (R 4.0.2)                      
   globals           0.14.0     2020-11-22 [1] CRAN (R 4.0.2)                      
   glue              1.4.2      2020-08-27 [1] CRAN (R 4.0.2)                      
   gtable            0.3.0      2019-03-25 [1] CRAN (R 4.0.0)                      
   GTSF              0.1.0      2021-02-18 [1] local                               
 V haven             2.4.0      2021-04-23 [1] CRAN (R 4.0.2)                      
   hms               1.0.0      2021-01-13 [1] CRAN (R 4.0.2)                      
   htmlTable         2.1.0      2020-09-16 [1] CRAN (R 4.0.2)                      
   htmltools         0.5.1.1    2021-01-22 [1] CRAN (R 4.0.2)                      
   htmlwidgets       1.5.3      2020-12-10 [1] CRAN (R 4.0.2)                      
 V httpuv            1.5.5      2021-04-23 [1] CRAN (R 4.0.2)                      
   httr            * 1.4.2      2020-07-20 [1] CRAN (R 4.0.2)                      
   jsonlite          1.7.2      2020-12-09 [1] CRAN (R 4.0.2)                      
   knitr             1.33       2021-04-24 [1] CRAN (R 4.0.5)                      
 V later             1.1.0.1    2021-04-23 [1] CRAN (R 4.0.2)                      
   lattice           0.20-41    2020-04-02 [1] CRAN (R 4.0.5)                      
   lazyeval          0.2.2      2019-03-15 [1] CRAN (R 4.0.0)                      
   leaflet           2.0.4.1    2021-01-07 [1] CRAN (R 4.0.3)                      
   lifecycle         1.0.0      2021-02-15 [1] CRAN (R 4.0.2)                      
   listenv           0.8.0      2019-12-05 [1] CRAN (R 4.0.0)                      
   lubridate         1.7.10     2021-02-26 [1] CRAN (R 4.0.2)                      
   magrittr          2.0.1      2020-11-17 [1] CRAN (R 4.0.2)                      
   memoise           2.0.0      2021-01-26 [1] CRAN (R 4.0.2)                      
   mime              0.10       2021-02-13 [1] CRAN (R 4.0.2)                      
   modelr            0.1.8      2020-05-19 [1] CRAN (R 4.0.0)                      
   munsell           0.5.0      2018-06-12 [1] CRAN (R 4.0.0)                      
   openssl           1.4.3      2020-09-18 [1] CRAN (R 4.0.2)                      
   parallelly        1.24.0     2021-03-14 [1] CRAN (R 4.0.3)                      
   pillar            1.6.0      2021-04-13 [1] CRAN (R 4.0.5)                      
   pkgbuild          1.2.0      2020-12-15 [1] CRAN (R 4.0.2)                      
   pkgconfig         2.0.3      2019-09-22 [1] CRAN (R 4.0.0)                      
   pkgload           1.2.1      2021-04-06 [1] CRAN (R 4.0.5)                      
   plotly            4.9.3      2021-01-10 [1] CRAN (R 4.0.2)                      
   prettyunits       1.1.1      2020-01-24 [1] CRAN (R 4.0.0)                      
   processx          3.5.1      2021-04-04 [1] CRAN (R 4.0.2)                      
   promises          1.2.0.1    2021-02-11 [1] CRAN (R 4.0.2)                      
   ps                1.6.0      2021-02-28 [1] CRAN (R 4.0.2)                      
   purrr           * 0.3.4      2020-04-17 [1] CRAN (R 4.0.0)                      
   R6                2.5.0      2020-10-28 [1] CRAN (R 4.0.2)                      
   Rcpp              1.0.6      2021-01-15 [1] CRAN (R 4.0.2)                      
   readr           * 1.4.0      2020-10-05 [1] CRAN (R 4.0.2)                      
   readxl            1.3.1      2019-03-13 [1] CRAN (R 4.0.0)                      
   remotes           2.3.0      2021-04-01 [1] CRAN (R 4.0.5)                      
   reprex            2.0.0      2021-04-02 [1] CRAN (R 4.0.5)                      
   rintrojs          0.2.2      2021-01-04 [1] local                               
   rlang             0.4.10     2020-12-30 [1] CRAN (R 4.0.2)                      
   rlist             0.4.6.1    2016-04-04 [1] CRAN (R 4.0.2)                      
   rprojroot         2.0.2      2020-11-15 [1] CRAN (R 4.0.2)                      
   rstudioapi        0.13       2020-11-12 [1] CRAN (R 4.0.2)                      
   rvest             1.0.0      2021-03-09 [1] CRAN (R 4.0.2)                      
   salesforcer     * 0.2.2      2020-09-09 [1] CRAN (R 4.0.2)                      
   scales            1.1.1      2020-05-11 [1] CRAN (R 4.0.0)                      
   sendgridr         0.1.0      2020-04-24 [1] Github (mrchypark/sendgridr@8da75ba)
   sessioninfo       1.1.1      2018-11-05 [1] CRAN (R 4.0.0)                      
   shiny             1.6.0      2021-01-25 [1] CRAN (R 4.0.2)                      
   shinycssloaders   1.0.0      2020-07-28 [1] CRAN (R 4.0.2)                      
   shinydashboard    0.7.1      2018-10-17 [1] CRAN (R 4.0.0)                      
   stringi           1.5.3      2020-09-09 [1] CRAN (R 4.0.2)                      
   stringr         * 1.4.0.9000 2020-05-30 [1] local                               
   testthat          3.0.2      2021-02-14 [1] CRAN (R 4.0.2)                      
   tibble          * 3.1.1      2021-04-18 [1] CRAN (R 4.0.2)                      
   tidyr           * 1.1.3      2021-03-03 [1] CRAN (R 4.0.2)                      
   tidyselect        1.1.0      2020-05-11 [1] CRAN (R 4.0.0)                      
   tidyverse       * 1.3.1      2021-04-15 [1] CRAN (R 4.0.5)                      
   tsibble           1.0.1      2021-04-12 [1] CRAN (R 4.0.5)                      
   twilio            0.1.0      2020-04-24 [1] Github (carlganz/twilio@ee0cbc5)    
   usethis           2.0.1      2021-02-10 [1] CRAN (R 4.0.2)                      
   utf8              1.2.1      2021-03-12 [1] CRAN (R 4.0.2)                      
   vctrs             0.3.7      2021-03-29 [1] CRAN (R 4.0.2)                      
   viridisLite       0.4.0      2021-04-13 [1] CRAN (R 4.0.5)                      
   withr             2.4.2      2021-04-18 [1] CRAN (R 4.0.2)                      
   xfun              0.22       2021-03-11 [1] CRAN (R 4.0.2)                      
   XML               3.99-0.6   2021-03-16 [1] CRAN (R 4.0.2)                      
   xml2              1.3.2      2020-04-23 [1] CRAN (R 4.0.0)                      
   xtable            1.8-4      2019-04-21 [1] CRAN (R 4.0.0)                      
   xts               0.12.1     2020-09-09 [1] CRAN (R 4.0.2)                      
   yaml              2.2.1      2020-02-01 [1] CRAN (R 4.0.0)                      
   zip               2.1.1      2020-08-27 [1] CRAN (R 4.0.2)                      
   zoo               1.8-9      2021-03-09 [1] CRAN (R 4.0.2)                      
StevenMMortimer commented 3 years ago

@carlganz Did you try assigning the output of sf_auth() to a variable and using that? For example, deleting your cached .httr-oauth-salesforcer file and running:

auth_data <- sf_auth()
token <- auth_data$token
access_token <- token$credentials$access_token

This should work because the sf_auth() function invisibly returns some things that might be helpful to you:

https://github.com/StevenMMortimer/salesforcer/blob/7751e6ccf76550048f514e7757accb94d6acb2a6/R/auth.R#L213-L216


Extra Note

The sf_access_token() will return null if you are using Basic Authentication (Username, Password, and Security Key)

carlganz commented 3 years ago

I did not!

I would strongly encourage you to add some documentation about the return value.

StevenMMortimer commented 3 years ago

@carlganz Were you using basic authentication and that's why it returned NULL and was confusing or was it something wrong with how sf_access_token() works? Just curious if it's a bug, or I just need to highlight the usage better? Thanks!

carlganz commented 3 years ago

I was using basic auth and didn't realize that session id is the Bearer token I need so I guess it's my own misunderstanding of Salesforce authentication. At the very least I would suggest documenting that sf_auth returns auth info and at best I would document meaning of each value returned.

As always, thanks for building and supporting this exceptionally useful package!