An alternative to Rob--W/cors-anywhere.
gcf-cors-anywhere is a Node.js proxy which adds CORS headers to the proxied request running on Google Cloud Functions.
The url to proxy is taken from the search params ?u=
, validated and proxied.
- In the Google Cloud Console, on the project selector page, select or create a Google Cloud project.
- Make sure that billing is enabled for your Cloud project. Learn how to confirm that billing is enabled for your project.
- Enable the Cloud Functions and Cloud Build APIs.
- Install and initialize the Cloud SDK.
- Update gcloud components:
gcloud components update gcloud init
- Deploy this function. YOU NEED TO CHANGE
PROJECT_ID
ANDGCP_REGION
.wget https://github.com/taichunmin/gcf-cors-anywhere/archive/master.zip -O gcf-cors-anywhere.zip unzip gcf-cors-anywhere.zip # YOU NEED TO CHANGE `PROJECT_ID` AND `GCP_REGION`. gcloud functions deploy cors-anywhere --allow-unauthenticated --entry-point=main --gen2 --max-instances=1 --memory=128Mi --no-user-output-enabled --project=PROJECT_ID --region=GCP_REGION --runtime=nodejs18 --timeout=60s --trigger-http --source ./gcf-cors-anywhere-master # clean up # rm -rf ./gcf-cors-anywhere.zip ./gcf-cors-anywhere-master
Use Axios and LINE Login API as example:
// YOU NEED TO CHANGE `PROJECT_ID` AND `GCP_REGION`.
const result = await axios.get('https://GCP_REGION-PROJECT_ID.cloudfunctions.net/cors-anywhere', {
params: {
u: 'https://api.line.me/v2/profile', // your real url
// you can add additional params here
},
headers: { Authorization: 'Bearer ACCESS_TOKEN' },
})
console.log(result.data)
// YOU NEED TO CHANGE `PROJECT_ID` AND `GCP_REGION`.
const result = await axios.post('https://GCP_REGION-PROJECT_ID.cloudfunctions.net/cors-anywhere', Qs.stringify({
client_id: 'CLIENT_ID',
id_token: 'ID_TOKEN',
nonce: 'NONCE',
user_id: 'USER_ID',
}), {
params: { u: 'https://api.line.me/oauth2/v2.1/verify' }, // your real url
})
console.log(result.data)