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

Oauth related example #123

Closed ghareesh closed 5 days ago

ghareesh commented 2 years ago

Hi Steve.. I am using salesforcer package 0.1.3.9000 in my prod server.. recently I migrated code to new platform.. But somehow this version is not available to pull from.. I used latest version by default.. but I am not able to fetch data using same.. mostly related to sf_auth.. Could you share example of how to change from simple user login to oauth based? I am doing like this.. Please correct options(salesforcer.consumer_key = "consumer-key") options(salesforcer.consumer_secret = "consumer-secret")

sf_auth(login_url = "https://vmware--sdpqa.cs24.my.salesforce.com/")

Once logged in, I clicked on allow access. After that I get below screen

Screenshot 2022-09-08 at 9 20 28 PM

Please help me understand how to proceed with API connectivity using oauth.. I am in catch20 here.. :(

ghareesh commented 2 years ago

Also I couldnt find cache file .httr-oauth-salesforcer generated in current working directory.. Is it fine to generate file and update as per some template? If so will behelpful if you can share sample..

StevenMMortimer commented 2 years ago

@ghareesh It sounds like you switched platforms and re-installed the {salesforcer} package, which installed the latest version (v1.0.1) that is different from what you were using in your prod environment.

Option 1

The easiest option, if you're happy using v0.1.3.9000, is to install a similar version from the CRAN archives or from a specific commit in the GitHub repo.

Example installing from CRAN archives

packageurl <- "https://cran.r-project.org/src/contrib/Archive/salesforcer/salesforcer_0.1.4.tar.gz"
install.packages(packageurl, repos=NULL, type="source")

Option 2

Instead of authenticating with an OAuth2.0 token, continue to authenticate using a username and password.

Option 3

We could try and troubleshoot how your connected app is setup. It looks like your error relates to the callback URL. The default is http://localhost:1410/, but maybe you need to set it to something else for the app you've created.

ghareesh commented 2 years ago

Hi Steve, as discussed over chat.. Unable to fetch data With new version haveing script out of bound error.

salesforcer- new version

Old version fetches it fine..

salesforcer0 1 3 9000 version

On Oauth issue, I will try configuring callback url and update you. Error I am getting there is error=redirect_uri_mismatch&error_description=redirect_uri%20must%20match%20configuration..

Call back url provided on app was - https://vmware--sdpqa.my.salesforce.com/services/oauth2/callback Hope I have to use same in options as below options(salesforcer.callback_url = "https://vmware--sdpqa.my.salesforce.com/services/oauth2/callback") sf_auth(login_url = "https://vmware--sdpqa.my.salesforce.com")

ghareesh commented 2 years ago

sfquery return with salesforcer 0.1.3.9000 version

Screenshot 2022-09-10 at 6 33 58 PM
ghost commented 2 years ago

@ghareesh It sounds like you switched platforms and re-installed the {salesforcer} package, which installed the latest version (v1.0.1) that is different from what you were using in your prod environment.

Option 1

The easiest option, if you're happy using v0.1.3.9000, is to install a similar version from the CRAN archives or from a specific commit in the GitHub repo.

Example installing from CRAN archives

packageurl <- "https://cran.r-project.org/src/contrib/Archive/salesforcer/salesforcer_0.1.4.tar.gz"
install.packages(packageurl, repos=NULL, type="source")

Option 2

Instead of authenticating with an OAuth2.0 token, continue to authenticate using a username and password.

Option 3

We could try and troubleshoot how your connected app is setup. It looks like your error relates to the callback URL. The default is http://localhost:1410/, but maybe you need to set it to something else for the app you've created.

Hi Steve

I'm Facing a similar issue as @ghareesh. The authentication works fine while running it in Rstudio on local machine. If I try to run the authentication from the Rstudio server, the callback URL defaults to http://localhost:1410/. How should I change the callback URL while working from the Rstudio server?

In {gargle}, the suggested way is to set use_oob = TRUE. Is there anything similar available here as well ?

Thank you in advance

StevenMMortimer commented 2 years ago

@ViswaDutt-P One option is to copy your local cached token file (named .httr-oauth-salesforcer) into the working directory of the script that is being executed on RStudio Server. Every time sf_auth() is run on the server, it will refresh the token automatically if expired and use that for the session (no interactive authentication required).

StevenMMortimer commented 2 years ago

@ghareesh Your query looks like a fairly complex relationship query. Back in July 2020, as part of v0.2.0, I updated the way sf_query() parses relationship queries to try and fix a bunch of other issues (#19, #35, #38, #54). It's possible that in fixing those, I've broken a case specific to your data.

In the short term, I'd recommending sticking with an older version of the package (maybe v.01.5?). In the long term, I'm happy to help troubleshoot this specific case. For example, take a look at the code that's behind sf_query() and run it step-by-step. Take the output of the httr_response object on Line 183, save it as an .rds object with saveRDS(), and email it to me for further debugging on how to parse the JSON or XML (depending on whether you use the REST or SOAP API). Or you are welcome to work on the parsing of the API response object if the data could be sensitive and you can't share.

The only way to know why the subscript error is occurring would be to look at how the response is being parsed.

ghareesh commented 2 years ago

@StevenMMortimer sure.. For short term fix u mentioned, in archive, I see 0.1.4, which I tested and is throwing same script out of bound error.. Could you refer me to location of 0.1.5? Long term , I will update you .. Will need time..

viswaduttp commented 2 years ago

@ViswaDutt-P One option is to copy your local cached token file (named .httr-oauth-salesforcer) into the working directory of the script that is being executed on RStudio Server. Every time sf_auth() is run on the server, it will refresh the token automatically if expired and use that for the session (no interactive authentication required).

@StevenMMortimer Thank you for the suggestion. I've tried it and works like a charm. just curious to know, is there any other option as well ?

StevenMMortimer commented 2 years ago

@viswaduttp The other option, instead of relying on the cached token, is saving the token itself and loading it like this to authenticate:

# Save token to disk and log in using it
saveRDS(salesforcer_state()$token, "token.rds")
sf_auth(token = "token.rds")

You also might be able to log in with just a username, password, and security token if your org doesn't require programmatic access (API access) to use multi-factor authentication. If you're not familiar with security tokens, there are a bunch of articles you can Google. Here is one that does a decent job at showing how to find yours: https://www.mcafee.com/blogs/enterprise/cloud-security/what-is-salesforce-security-token-and-how-do-i-find-it/

StevenMMortimer commented 2 years ago

@ghareesh Apologies, if you're having trouble with installing from the archives. It looks like the only versions in the archive prior to v0.2.0 are v0.1.2, v0.1.3, and v.0.1.4 so you're welcome to try any of those. A better alternative, might be trying to install the exact version of what's on your prod server. To do this I'd recommend running library(salesforcer) then running devtools::session_info() to get a dump of the package versions you're using. You should be able to see, if you installed directly from GitHub, which commit you installed from. The example screenshot below shows I have commit 0f7d33a installed from the r-lib/revdepcheck repository).

image

After finding the salesforcer commit that you've installed, then you can simply re-install from that specific commit again using this command:

# install.packages("remotes")
remotes::install_github("StevenMMortimer/salesforcer", ref="COMMIT HASH HERE")
ghareesh commented 2 years ago

@StevenMMortimer I tried with 0.1.4 since it was only version available next to what I had in server (0.1.3.9000).. Even it was not working.. So I just copied 0.1.3.9000 version from lib file of server and put it on to my new platform.. It seems to be working fine.. But my worry is in case it becomes non compatible with latest R versions in future.. will have to start from scratch again for resolving issue..

StevenMMortimer commented 2 years ago

@ghareesh Copying the lib folder works too!

This gives me the idea that I should add a function argument that will allow users to export the raw JSON or XML when running sf_query(). That might make it easier to share/reproduce for me or debug the issue on your side if the data is sensitive.

Keep me posted if you run into more issues.

ghareesh commented 2 years ago

@StevenMMortimer Sharing is not an issue.. Given my non coding background, I find it hard to understand function .. Given I am using REST method, can you suggest a trimmed down version of function for easier execution.. I tried running only sf_query_rest And it gives Error in is_present(page_size) : could not find function "is_present"..

Below lines what should be filling values? control <- filter_valid_controls(control)

query_url <- make_query_url(soql, queryall, next_records_url)..

May be getting into call can fix this sooner.. Thoughts?

StevenMMortimer commented 2 years ago

@ghareesh is_present() is a function from the lifecycle package. Have you installed that?

StevenMMortimer commented 5 days ago

Closing due to inactivity. @ghareesh Re-open if needed or create a new issue.