Creating refresh tokens for Sharepoint REST API from Matlab - matlab

I'm using Matlab's webread function to read lists from my organization's Sharepoint. I went through a detailed process to generate a token in Postman (with client_id, client_secret, etc). I then send this token via the webread function and this works perfectly in reading the SP lists. So far, so good.
However, the Sharepoint access token expires every few hours. I manually create a new one in Postman each time and manually paste it in my Matlab script. I've read about creating longer-lasting 'refresh' tokens that are generated automatically. Being mainly a Matlab user and not an I/T person, I cannot for the life of me figure out how to create these refresh tokens.
A second (less desirable) alternative would be to use catch/try to check if the current token has expired and create a new one. That command is also quite complicated needing realm, client-id, client-secret, etc. Here also, I cannot figure out how to structure the webread argument to send all this information.
Has anyone has successfully kept a continuous REST API connection from Matlab to Sharepoint without expiring tokens? Are there any other (better?) alternatives to using tokens? I'd greatly appreciate a gentle, step-by-step procedure on how to do this.
TIA
Succesfully generate the initial token, seeking help on a non-expiring REST API connection between Matlab and Sharepoint.

Related

Need to find a way to automate ADF bearer token with a python script

I am looking to automate the access token information available through this link:
https://learn.microsoft.com/en-us/rest/api/datafactory/pipeline-runs/query-by-factory?tabs=HTTP
by either hard-coding my subscription information or through any other services.
Can anyone guide me into finding solutions for this. I have been reading into service principal and postman but I have never used these tools (is this something that is doable)
I want to be able to assign the bearer value to a variable in python.

HTTP 401 Unauthorized when export report by using PowerBi rest api

I got 401 unauthorized when trying to export a report from Power BI online by using the Power BI REST API.
My access token works fine with other API methods. I am also setting full API access in App registrations.
When I logged in by using the same account and use 'try', the access token provided in this test actually works.
https://learn.microsoft.com/en-us/rest/api/power-bi/reports/exportreport
Has anyone successfully used this method?
You will need to create an Authentication app and provide the right privileges corresponding to the REST API you are loading from. For this particular API Microsoft suggests to provide Report.Read.All or Report.ReadWrite.All privileges.
You can achieve this directly by creating an authentication application from https://dev.powerbi.com/apps.
You will also need to make sure you grant the admin consent once your app is up and running.
Also, make sure the request you are creating is in the right format. If you match your request with the one specified here : https://learn.microsoft.com/en-us/rest/api/power-bi/reports/export report and test to check if it working with your parameters and login, you should be able to make it work with your code. Also test it with Postman to make sure you are entering the right configuration in the request and you know what you are requesting.
Once you have all this in place, you should not have any issues accessing the right data.

How to call Salesforce REST API from external web forms

I am a bit confused. The requirement is that we need to create a REST API in Salesforce(Apex class) that has one POST method. Right now, I have been testing it with POSTMAN tool in 2 steps:
Making a POST request first with username, password, client_id, client_secret(that are coming from connected app in Salesforce), grant_type to receive access token.
Then I make another POST request in POSTMAN to create a lead in Salesforce, using the access token I received before and the body.
However, the REST API that I have in Salesforce would be called from various different web forms. So once someone fills out the webform, on the backend it would call this REST API in Salesforce and submits lead request.
I am wondering how would that happen since we can't use POSTMAN for that.
Thanks
These "various different web forms" would have to send requests to Salesforce just like Postman does. You'd need two POST calls (one for login, one to call the service you've created). It'll be bit out of your control, you provided the SF code and proven it works, now it's for these website developers to pick it up.
What's exactly your question? There are tons of libraries to connect to SF from Java, Python, .NET, PHP... Or they could hand-craft these HTTP messages, just Google for "PHP HTTP POST" or something...
https://developer.salesforce.com/index.php?title=Getting_Started_with_the_Force.com_Toolkit_for_PHP&oldid=51397
https://github.com/developerforce/Force.com-Toolkit-for-NET
https://pypi.org/project/simple-salesforce/ / https://pypi.org/project/salesforce-python/
Depending how much time they'll have they can:
cache the session id (so they don't call login every time), try to reuse it, call login again only if session id is blank / got "session expired or invalid" error back
try to batch it somehow (do they need to save these Leads to SF asap or in say hourly intervals is OK? How did YOU write the service, accepts 1 lead or list of records?
be smart about storing the credentials to SF (some secure way, not hardcoded). Ideally in a way that it's easy to use the integration against sandbox or production changing just 1 config file or environment variables or something like that

Refreshing an Access Token vs obtaining a new one

I am writing a some PowerShell scripts to log into Azure AD using a clientid/secret.
The code for obtaining a new access / refresh token is very simple. I have a Get-Token function with all the error checking etc in about 80 lines of code.
If I write Get-Token so that it will decide whether to get a new token, send an existing one or to obtain a new one with a refresh token would invariably be considerably more complex.
Is there any benefit to renewing tokens rather than simply obtaining a new one each time the function is called? (considering there is no consumer side to this, it is a backend script to connect to Azure services)
ETA
This isn't using ADAL, it is using raw HTTP requests, so I'm managing the tokens directly.

php script to validate multiple facebook access tokens

I am trying to find a way to automatically validate multiple facebook access tokens at once.
I am storing user access tokens into sql database and (as expected) after some time some of the tokens will expire or becomes invalid. (due to user change password/de-authorized app, log out, etc.)
Is there any way to check/validate all access tokens from a .txt file/database and delete those tokens that are invalid: doing this manually for more than 100 tokens is very difficult.
Options
checking access tokens from a .txt file (using PHP) and remove invalid tokens simultaneously so that at the end we will have a list with only valid tokens.
directly remove invalid tokens from the database.
Any insight?
This has become a very common problem now a days not only me but most of us are still googling like i did before posting it here but no one has ever found a way to solve this problem and i believe is this is the only forum where this thing can be fixed.
No, there is no endpoint where you could check multiple tokens at once. The most similar method to achieve the functionality you want is to batch the requests. You should be good to batch them in sets of 25.
So, your flow would be this: Get 25 tokens from the SQL database. Call /me endpoint with each token. IE, your batch call with look like
batch=[{"method":"GET","relative_url":"me?fields=id&access_token=TOKEN1"},{"method":"GET","relative_url":"me?fields=id&access_token=TOKEN2"},...,{"method":"GET","relative_url":"me?fields=id&access_token=TOKEN25"}]
Loop through the response, if its checks out, you're good. If an error is thrown, its not. Deal with each access_token accordingly. The responses will be in the same order as you gave Facebook.