You can use Passport for this.
Best practice for an api service
There are so many pieces to laravel, I'm still learning them all. I'm looking for suggestions as to best method/design pattern would be for handling a handle to an outside web-service authenticated with oauth2.
Basically, the web-service requires a confirmation via oauth2 then, once verified, you request a short-term 'access_token' that's good for 1 hour and can be renewed. I can think of a number of ways I would code for this myself, but I'm not sure what the 'laravel way' might be. Once I get an access token for a given scope, there's no reason to get a second one if I'm using the same 'scope' until the existing one expire. (and it can be refreshed)
Should I handle this with a 'model' saving my nonce as the identifier key? Use a singleton service or repository? Save the details in a session cookie? What is the recommended way of handling this sort of thing?
SW
@vincent15000 it looks interesting, but as per usual, the laravel docs are too vague "this is all of what it can do..." without detailed specific instructions for specific applications. And looking around the net, I have yet to find a good example of creating a 'client' (most of the cost examples I'm seeing so far involve creating a server/API)
Do you know of any good tutorials involving building a client in it? I was able to get a branch with passport installed, tables migrated and while some of the 'do this step' instructions either failed or didn't seem to apply to what I need, I was able to add a record in oauth_clients.
The laravel docs tell you how to do this, but then say nothing about actually referencing that outside of 'here's how you can get a list of clients a user has access too.' Um ok, that's like the old military manuals that say "here's how you can make something explode" without telling you how to get far enough away not to go with it! lol
@treii28 are you making a Web that provide Oauth2 authentication, or do you want to authenticate YOUR web with an Oauth2 provider?
Or, in other word, do you want to do [a "sign-in with facebook" button on your app] or do you want [everyone can create a "sign-in with your treii28's app account" button?]
Passport is a package to make your web into an Oauth2 provider, like facebook, github, google , etc.
If you just want to implement a "sign-in with" button, look for Socialite
@Lumethys I want to create a laravel interface to the client to talk to Etsy. I need to pull recent order details for specific items. I was able to hack up some crude guzzle scripts to understand the process. Now I want to put it into laravel as the result needs to go back into another api client I built to create an order on another website.
I find tons of examples for setting up servers, but I'm not finding any for setting up a client.
@treii28 so basically you want a login with Etsy button on your site, then your site will read Etsy's data, right?
Then you should use Socialite not Passport
Passport is what you would use if you want to create something like the Etsy website
Socialite do not have first-party support for Etsy, however, they do promote a community website that provide additional adapter in their docs: Socialite Provider.
They have an adpter for Etsy: https://socialiteproviders.com/Etsy
Also give the official document on Socialite a read: https://laravel.com/docs/9.x/socialite
@Lumethys OK, cool. Yeah, i started looking deeper into the 'Client' code in laravel and wasn't finding links to anything that actually did any rest calls. it looks like it's storage of credentials only. (I couldn't even find anywhere to enter the remote urls)
And I already have that in my own tool. But I'll take a look at socialite as I may have to build still more of these in the future. As long as I'm getting my fingers dirty with replacing what printify is doing for me on the more basic products, I may as well see who out there can do something similar. I might even be able to cut printify out as the middleman with some printers when printify doesn't offer their full product lines. (one of the items that drew me to printify in the first place is offered by a company in china that has many similar items I'd love to use, but printify doesn't currently support them)
Hmmm, looks like Socialist is only built up for the user details at the bare-bones level. But I might be able to extend it for the shop functionality. thanks for the tip.
yeah, looks like I would need to build a new Provider and then add relationships to an extended (or completely re-built) User for socialite.
Most of the OpenAPI/Swagger regenerated code makes a base User type class and then adds the various functionalities off user and it's sub-parts. e.g.
// abstract example only - not real code
$user = new API\User();
$shops = $user->getShops();
$listings = $shops[1]->getListings();
// etc
There were also a couple of open-source esty-api packages on github that I've pulled down, but those I've found aren't complete and don't support all I need. But they at least make good examples to cut/paste from.
I just cache the access token with the cache expiry set a little shorter than token expiry time. If the token is still in the cache, use it, otherwise get a new one.
@Ben Taylor that's what I did in my guzzle scripts mentioned above in my resposne to lumethys. I want to put it all into a laravel admin page now so I just click a button and retrieve any recent order details for a specific item.
Basically, it's a product the customer wants personalized in two ways. It's for class reunions and thus the result needs to show both the year the person graduated and their last name (at graduation). So I need to scrape etsy for orders on effected items, get the product details along with the year and name, then create an order on another website with the custom details. (I basically set up code already to 'create' a design from an existing template for custom t-shirts that look like sports jerseys. the year will be the big number on the back, with the last name across the top)
I don't understand all ... you need to connect to a service with OAuth2 ? or do you need to create your OAuth2 service to provide this connection to others ?
@vincent15000 I sell things on etsy. Among the things I sell are designed t-shirts and similar items printed/fulfilled via printify.
I want to use one of my parked domains to set up a domain interface for taking order information from Etsy, manipulate it and post the order to printify.
Full details: Someone has asked me to make a product for attendees to an annual 'class alumni' party for my old high school. Alumni from multiple class years show up in the hundreds to these parties, and it has apparently been an issue identifying people you know when 30-50 years have passed since you last saw them. Thus he wants me to see if i can make shirts available that look like the old school sports jerseys with the class year printed in place of the player number, and the alumnist's last name at time of graduation printed across the back.
Printify has support for personalization, but they don't have support for [many] other 'configurable' options. (the options vary by design and what front-end 'shop' you are linking it too)
Among other things, we'd like to offer a couple different styles of the shirts in a couple color combinations for people to choose from. Doing it with the printify design tools and their existing Etsy integration would require me designing a separate shirt for every year, every color and every style. 30+ years x 3-4 styles x 3-4 color combinations for a couple hundred listings. I would also need to manually transfer the 'name' to the designs once they showed up on the printify side.
Besides the fact Etsy charges 'per listing' (a very small amount - something like $0.20 every 2 months), it would make for an unmanageable store for the buyers since they would have to hunt out their class year and sizes among literally hundreds of nearly identical listings. (yes, Etsy has 'keyword' type grouping, but a) some people won't notice that and b) I would have to set up all of the designs and keywords and listings, etc. one at a time by hand - pain in the buttocks!)
Meanwhile, Etsy only has support for 3 'options' per listing. Size ends up eating one of them. Personalization a second. That would leaving me one selector for the year. But adding the selector for a year, breaks the integration between Printify and Etsy currently managed on the Printify side.
I've already written code to generate a custom design (on printify) when given a name and a year. It can also vary the colors and I know how to apply it to different shirt styles also with a little more tweaking. I've already looked into how you can then use the same printify api to create an order, add the design and submit it.
Now I need to build a tool to scrape etsy, get the orders that match, pull the size, name and year (and eventually the style and colors) to then create the 'custom' design on printify, add it to an order and submit the order. It's likely I will also then go 'the other way', likely with some kind of scheduled job, to scrape printify to get things like order status, tracking numbers, etc. then post that onto the Etsy system. (all things currently handled by Printify on the simpler stuff that won't "break" their integration)
Printify only requires a bearer token on their API, and as mentioned, I already have most of the code finished that does what I want. I'm just starting on the Etsy code and it required using a web interaction to authorize the api token, so I started building the website backend earlier than planned using laravel.
Before I go build my own guzzle based client from scratch, I posted here to find out what the 'best practices' way was in laravel for building such a thing as a service or repository or whatever. Someone then suggested using Passport, so I started looking into that but found no documentation on how the 'client' piece is actually utilized.
Assuming I can also get it tied into something like Laravel in some way, I can also create a limited feature access to some of the data I'm storing to keep track of orders run through my code so that the person running these parties can also see basic details. (what's been ordered by whom, etc)
@treii28 Sorry too long to read in details tonight, I read your answer tomorrow. I just read your last answer and some short parts of your previous answer. If you just need to handle datas with other services, why do you want to provide an OAuth2 access ?
What I understand is that you need to handle data on Printify and Etsy by using their OAuth2 access. So you only need to code this access from your Laravel app which is a client which will access those services.
Can you confirm please ?
@vincent15000 basically, I already had part of an api built. I was just wondering (initially) how most folks incorporate that sort of thing into laravel. (as mentioned, do I do it as an independent library, a service, a repository, or something else I haven't uncovered yet?) Someone pointed out passport in the first response and I'm still trying to figure out if it even works as a client interface or just stores values related to one. I've gone back to working on my own guzzle based lib for the time being and I'm still using a laravel model to store all the codes and tokens for the time being. it seems to be working, but isn't likely to resemble 'laravel mainstream' methods, whatever they may be. I was mainly wondering the 'recommended way' to do such a thing so anything I build won't become obsolete with the next update(s) to laravel
Personally, I make a service class for the api or several if its complex. Imagine its a model, and thats how I interact with it. map apis, data apis.
The counterpart api usual lends to how I structure my end (in your case Etsy). If they have several endpoints I will make a folder with a base class that has the Http/Guzzle/Client then I use it in the separate classes. If its something simple one file... Services/Flickr.php
@webrobert thanks, that's kinda what I was wondering.
I've played with repositories previously, and while I mainly used those for accessing database content, I'm going to ultimately be using this one in a similar fashion. (to look up/modify order details) I figured it might be well suited to a repository with a service back-end to scrape 'new' information, but mostly dealing with local 'copies' of information scraped previously or concurrently. Presumably, my application won't have to access the same 'order' details more than twice - once coming in and once, maybe twice going back once the order details have been created on the fulfiller side and any tracking details are added.
@treii28 Sorry for my mistake, I didn't understand exactly what you need. Perhaps I have yet progress to do in english. Well ... yes as @webrobert said, a Service is probably a good idea.
@vincent15000 actually, a great many of the comments in here have been most helpful. Since I'm so new to laravel (and trying to learn about it for work, thus is why I'm not building my personal stuff in it) all related information is useful.
I may well have to build an API for various customers to access as well as for our own internal stuff between the website and production facility. I may want to add 'sign in with google' type support to the main website if the developer working on it doesn't do it themselves.
I'm an info junky! And I can also consider selling my own personal code at some point if and when I come up with something that would be useful to others. (a tool that sat between Etsy [and other storefronts] and Printify for more custom personalization doesn't yet exist and while manual personalization was hacked in by printify, it's still not automated)
Among other things, in this process, i ended up having to build a tool modified off an open source package called 'EasySVG' that allows me to generate svg-path based text using any font I can convert to svg font format. That piece is handy in and of itself!
SW
9 Trending Best Practices for REST API Development
https://www.partech.nl/nl/publicaties/2020/07/9-trending-best-practices-for-rest-api-development
Please or to participate in this conversation.