Replies
|
Hi, yes this is possible to do with YARP.
As long as the browser can make an HTTP request to it, YARP doesn't care that it came from a Blazor browser app.
I would recommend you take a look at the minimal sample. The only modification you should need is to the config file. Your config may look something like "ReverseProxy": {
"Routes": {
"catchAllRoute": {
"ClusterId": "twitterCluster",
"Match": {
"Path": "{**catch-all}"
},
"Transforms": [
{
"ResponseHeader": "Access-Control-Allow-Origin",
"Set": "*"
}
]
}
},
"Clusters": {
"twitterCluster": {
"Destinations": {
"twitterV2": {
"Address": "https://api.twitter.com/2/"
}
}
}
}
} |
6 replies
Answer selected by JeepNL
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What I want to do:
Making a call to the Twitter API directly from within a static Blazor WASM front-end client (the browser), so without calling a back-end API which makes a call to the Twitter API.
The problem
The Twitter API (v1/v2) doesn't support CORS (yet, they say they are looking into it) they only support back-end (server to server) calls to the Twitter API. So the Twitter API doesn't add the
access-control-allow-originto the response, and when it comes back the browser won't accept the response (preflight & fetch) because of this:Access to fetch at 'https://api.twitter.com/2/tweets?ids=1503704881361735691&tweet.fields=text' from origin 'https://localhost' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.But with Blazor WASM I think it would be a good idea to make the calls directly to the Twitter API from within the client, so skipping the back-end API step, taking up less server resources, let the user's browser do the hard work. And yeah, this sure does scale😉
Client/Auth Safety
Twitter API V2 does support a "Public Client" scenario to authenticate, where you only need to send your Client ID and no secret. You then can get an
AccessToken(= Bearer, valid for 7200 seconds) and an optionalRefreshTokento authorize further calls to the APIA Possible Solution?
I've read at Stack Overflow that people use a reverse proxy to do this, so I directly thought about using YARP for this.
I'd like to use YARP to do this:
<query>+<authorization header>) the Twitter API v2 needs.https://api.twitter.com/2/<query>with theauthorization: Bearer <Access Token>request headerhttps://api.twitter.comAccess-Control-Allow-Originheader to the response.Where I am now
I've read the YARP documentation, especially Direct Forwarding, Header Routing and Cross-Origin Requests and searched the YARP Q&A Discussions. It's a lot of information and I don't know exactly where to begin but I don't mind putting more effort and time learning in it.
My Questions
Thank you,
Jaap
Hi, yes this is possible to do with YARP.
As long as the browser can make an HTTP request to it, YARP doesn't care that it came from a Blazor browser app.
I would recommend you take a look at the minimal sample.
The only modification you should need is to the config file.
You must specify the correct destination, and add a Transform that adds the header.
Your config may look something like