How to get OAuth token in Business Central Codeunit (al code) - dynamics-business-central

We have been searching and searching to find examples of how to get an OAuth token in al code in our codeunit. We can successfully get the token in Postman, so I know all the credentials are correct. But finding examples on the web of how to do this has proven to be extremely difficult.
I did find this .al page from Microsoft that looks like it has an example using OAuth2.AcquireTokenWithClientCredentials but we can't get it to work. https://github.com/microsoft/BCTech/blob/master/samples/OAuth2Flows/TestOAuth2Flows.Page.al
Does anyone have a working example of this?

Kauffmann has a good blog post on this. It's talking about calling BC with an OAuth token, but the same principle applies.
Are you able to get the OAuth token in AL, and just have trouble using it to authenticate the request? If so, try this from the blog post:
Client.DefaultRequestHeaders.Add('Authorization', GetAuthenticationHeaderValue(AccessToken));
Client.DefaultRequestHeaders.Add('Accept', 'application/json');
If you can't get the token, I'd recommend looking at the OAuth2 codeunit.

Related

Invalied token error in google calendar api

I'm using google calendar api to addd events tocalendar. When I'm using oauth2.0, its asking for oauth and working properly. but after 2 0r 3 try, its failing, it shows invalied token error. And again after some times it work properly.
And if I use the token stored, its asking for oauth2 for the first 1 or 2 attempts and its fine. but its not asking for oauth and no events are being created. agin after some tries its asking for oauth2.
I didn't get any Idea why its happening so. Is there any solution?
This is probably occurring because your tokens are expiring. Include access=offline and prompt=consent in the authorization code link and it will probably start to work. Also make sure to refresh your token of the original access token fails and call the API with the new access token.
I work with Pathfix and it takes off the exact hassle for you if you want to save some time with the OAuth dance :)

Google Oauth flow in Meteor

Within a Meteor app, can anyone guide me to how to allow users to simply Oauth Google?
There's the accounts-google package, but I don't want users to be able to login with it, just oauth and store credentials.
The docs mention "If you just want to authenticate to an Oauth service like Twitter, Facebook, or Google without using Accounts – that is, if you don't want to log the user in, you just want an OAuth token – you can use the underlying service packages such as twitter, facebook, and google directly.", but there are no docs whatsoever for the google package.
Any guidance would be much appreciated.
The Oauth's package is intended to do just this. If you want to use it to get people's credentials
There are two options:
1) Modify the source of the google and accounts-google packages to remove the parts that log you in. There are no docs for this, i'm afraid, the best to understand it are the inline comments.
2) Use another atmosphere package to merge accounts-google with an existing login. So they're already logged in but can use Meteor.logInWithGoogle() to login with google and this is merged with their existing account. This way they don't have to log in with Google but you get to store the OAuth token. For this you can use the bozhao:link-accounts package, or the mikael:accounts-merge package:
// ON THE CLIENT:
Meteor.signInWithGoogle ({}, function (error, mergedUserId) {
// mergedUsers is set if a merge occured
if (mergedUserId) {
console.log(mergedUserId, 'merged with', Meteor.userId());
}
});
Then you would find the google OAuth token data in services.google on your existing Meteor.user() document.
It's fairly straight forward to handle the Google OAuth manually. Check my answer on this (similar) SO question
Getting OAuth tokens without user registration

Linkedin API call C#

I need to access Linkedin api using C#, but not able to get documents/articles how I can make call to Linkedin API.
I need to get company information using Linkedin API if I pass the company name I need to get the feeds.
Can anyone please direct me how I can proceed further.
Regards
Anand
This call for get the code:
https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=your-client-id&redirect_uri=your-redirect-uri&state=987654321&scope=r_basicprofile
This call for exchange code to token:
https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=your-code&redirect_uri=your-redirect-uri&client_id=your-client-id&client_secret=your-client-secret
And finally this call for get user info:
https://api.linkedin.com/v1/people/~:(firstName,lastName,picture-url)?oauth2_access_token=your-token&format=json
To take the client id and the client secret, you need sign up to linkedin developer, Here:
https://www.linkedin.com/secure/developer

Get linkedin public status updates

I am trying to get latest 3 public activities of our company from linkedin (ex: http://www.linkedin.com/company/4043/activity) and present it on our website but unfortunately I dont see a clear explanation to do so in the API documentations. I registered an API Key but I am confused if I need to do OAuth if so against what and then what API to call to get publicly available info?
Is OAuth really needed for something that is available?
Can I store an OAuth token for an internal user and keep fetching data on that? I would really do not want to authenticated the site visitors with oauth to get free linkedin content.
This question is on the lines of this one, but I still wanted to ask and see if I could get a better response/answer.
To get the company updates from the LinkedIn API, you will need to reference the following document:
https://developer.linkedin.com/reading-company-updates
The API is currently in preview mode, but the above document addresses your question re: OAuth as well as shows the specific calls as well as sample output. If you have any questions after reviewing the docs, please feel free to visit our developer forums at:
https://developer.linkedin.com/forums/rest
And one of someone from our team would be more than happy to help you further.
Enjoy!
-Jeremy

401 Authentication issue using twitter api with Get Method (oAuth)

I want to search microblogs from twitter using
http://api.twitter.com/1/users/search.xml
using following header to authenticate the request.
{Authorization: OAuth oauth_nonce="NjM0NTU3NjkwODQ1NjI1MDAw",
oauth_signature_method="HMAC-SHA1",oauth_timestamp="1320201086",
oauth_consumer_key="Key Goes Here",
oauth_token="Token goes here",
oauth_signature="D%2FEWfy3qEr5QiEzSvtcYhBDs864%3D",
oauth_version="1.0"
}
but it give me 401 Authentication failed.
Can anybody help me to get out of this.
I would suggest you look into using a pre-made library to handle the Twitter API calls, or, at the very least, OAuth.
In my opinion, it will not be worth your time to work through the many steps required for OAuth request signing.
There are many libraries available (list swiped from dev.twitter.com with inactive projects omitted):
DotNetOpenAuth
Hammock
LINQ to Twitter
OAuth-Dot-Net
Twitterizer
Full disclosure: I am the author of Twitterizer.

Resources