I have a website that adds dns records to itself automatically using the cloudfare api.
So here is the fetch request that I do:
fetch("https://api.cloudflare.com/client/v4/zones/***{ACCOUNT_ID}***/dns_records", {
body: '{"type":"A","name":"example.com","content":"127.0.0.1","ttl":3600,"priority":10,"proxied":true}',
headers: {
"Content-Type": "application/json",
"X-Auth-Email": "********#gmail.com",
"X-Auth-Key": "************"
},
method: "POST"
}).then(res=>{res.json().then(r=>console.log(r))})
So the api key I am using is generated on the api zone keys, with perms to edit the zone that I am looking to. However, it gives me this:
{
success: false,
errors: [ { code: 10000, message: 'Authentication error' } ]
}
I have tried using X-Auth-User-Service-Key instead of X-Auth-Key and excluding the email, I have tried using my global api key as X-Auth-User-Service-Key and X-Auth-Key, I have tried using my local key, but nothing I do seems to work. I just get the same error. I have tried replacing the account id in the url by the seemingly random combination of letters and numbers provided in the cloudfare api docs, but still, nothing seems to work. Can someone tell me what I am doing wrong?
Related
I am using getServerSideProps to fetch data from my firebase database into my Next.js application.
My code snippet looks like this:
export async function getServerSideProps(context) {
const session = await getSession(context);
const products = await fetch("https://database-73695.firebaseio.com/").then(
(res) => res.json()
);
return {
props: {
products,
session
},
};
}
The problem is that I get error message saying the following: "FetchError: invalid json response body at https://database-73695.firebaseio.com/ reason: Unexpected token F in JSON at position 0"
I have seen that some people report this error when the data fetched is actually text and not an object. I tried changing the response from res.json to res.text, but then I'm told that "text is undefined".
Does anybody have any idea of what could be happening?
UPDATE:
By testing different fetching methods, I have seen the error:
Firebase error. Please ensure that you have the URL of your Firebase Realtime Database instance configured correctly.
All fetching code (with or without getServerSideProps) work when used with other APIs.
My database URL comes from Firestore, and is formated as follows:
https://PROJECT-ID.firebaseio.com
It is located in us-central, which I know is important for the URL.
Something else that might be worth noting: the database has already a collection called "users" tied to Stripe transactions, which works.
Any ideas?
Thank you for your time.
->try adding headers:
headers:
{
Accept: 'application/json, text/plain, /'
'User-Agent': '*',
},
->try checking if data is not been fetch from the back-end
Im trying to make a discord bot where if you type -cr into the chat, it takes the Arguments of the user (Being the Clash Royale Player's player tag) and would then use the package node-fetch to receive data with my specified endpoint. I am constantly running into the error of { reason: 'accessDenied', message: 'Invalid authorization' }. Im rather new to this stuff, especially API's, but im hoping to access certain data which I can decide later on (Which I know how to do). My code is :
const fetch = require('node-fetch')
module.exports = {
name: 'clash',
aliases: ['cr', 'clashroyale'],
category: 'This',
utilisation: '{prefix}clash',
async execute(client, message) {
var msgArgs = message.content.slice(this.name.length + 1)
var endpoint = `/players/${msgArgs}`
var url = `https://api.clashroyale.com/v1`
var token = `hidingmytoken`
fetch(url + endpoint, {
method: 'POST',
headers: {
"Authorization": token
}
}).then(data => data.json()).then(json => {
console.log(json)
})
},
};
The message parts with msgArgs and discord sides all work but fetching that clash Royale API is a big hurdle for me. The API for Clash Royale can be found here https://developer.clashroyale.com/#/documentation and Im just generally stuck on this whole concept. Im using version 2.6.6 of node-fetch so I can use the require() method which should work if that does matter. In general, how can I pass my token properly to receive that API data?
Since the Clash Royale API uses bearer authentication, you need to specify that it will be a bearer token.
headers: {
'Authorization': `Bearer ${token}`
}
I've implemented the following functionality. The code is written in GO but you can copy the logic and translate into your language.
The library have the following functionality:
Login
Token generation
Token list
Token delete
https://github.com/alessiosavi/GoClashRoyale/blob/master/api/auth.go
Hi I'm doing a coding challenge and I'm trying to fetch data from an html page but I keep getting the 400 error. I don't see where my syntax would be wrong. The Promise returns an empty string as PromiseResult. Why do I not get the data from https://adventofcode.com/2021/day/2/input?
fetch('https://adventofcode.com/2021/day/2/input', {
method: 'GET',
mode: 'no-cors',
credentials: 'omit',
headers: {
'Content-Type': 'text/plain'
}
})
.then((response) => {
response.text();
})
.then((html) => {
var parser = new DOMParser();
var doc = parser.parseFromString(html, 'text/html');
})
.catch((err) => { console.log(err) })
After visting https://adventofcode.com/2021/day/2/input I get information that
Puzzle inputs differ by user. Please log in to get your puzzle input.
Please check if you are providing your login data correctly.
mode: 'no-cors',
This says that you are not going to do anything that requires permission to be granted using CORS. Anything that does require CORS will be quietly ignored.
You need CORS permission to read data across origins. Since you have said you aren't going to use CORS, you don't have permission, so you don't get any data.
This is why you get an empty string.
credentials: 'omit',
The input endpoint requires your credentials so that it can give you your input, which is unique to each user. Since told the browser not to send any, the end point doesn't understand the request.
As an aside:
headers: {
'Content-Type': 'text/plain'
}
This is just nonsense.
It claims that the request includes a body consisting of plain text.
You are making a GET request. There is no body on the request at all.
Advent of code expects you to manually download your input data. It doesn't expect your solution to fetch it from the AoC website.
JS solutions are generally run in Node.js (rather than a web browser) where they can use the fs module to read the local copy of the input file. (Tip: I find it handy to copy/paste the sample data into a sample file to test my results against the worked example on each day).
How filter a http response with MailChimp API V3 ?
I'm working in NodeJS. I'm trying to filter the response of my "GET" request in MailChimp API.
Here my snippet.js :
var getList ="https://us18.api.mailchimp.com/3.0/lists/6b3d443365/members"
request({
url: getList,
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Authorization': 'apikey ' + password
},
fields: ["id"], // I have also tried: "Fields"
}, function(err, res, body) {
if (err) {
return console.log("err:", err)
}
console.log("connection succeed");
console.log("res: ", body)
});
I have tried also :
1-
fields:["members.id", "members.email_adress"]
2-
fields:"id"
res : BIG BUNCH of data
all my tentative fails, my console returns me a big bunch with all the data in. I want to filter it but I don't have a clue how to make it. The documentation affirm :
Query string parameters
fields:
Type: Array
Title: Fields
Read only: false
A comma-separated list of fields to return. Reference parameters of
sub-objects with dot notation.
It seems to me I have followed the notice, so, what's going wrong ?
The documentation does in fact say that the 'fields' parameter is
A comma-separated list of fields to return. Reference parameters of
sub-objects with dot notation.
https://mailchimp.com/developer/reference/automation/
The key information that is missing is that you need to prefix each field with a (redundant) reference to the root object.
/3.0/automations?fields=automations.id,automations.settings.title
Getting specific fields from a query (lists) I had to do the following:
/3.0/lists?count=100&fields=lists.name,lists.id
I agree with the above comment. The Mailchimp API has HORRIBLE documentation.
I know the question is a bit stale, but for the benefit of others who might find this helpful...
The Mailchimp API docs suck when it comes to examples for requests with query strings. I'm not using nodejs but PHP and Curl and was having a similar problem specifying the exact fields that I wanted returned from the API.
After many hours of trial and error I found that the ONLY query string format that works is simply: fields=fieldname1,fieldname2,etc. I tried fields=fieldname1&fields=fieldname2 and (PHP specific) fields[]=fieldname1&fields[]=fieldname2 and fields=[fieldname1,fieldname2] and fields={fieldname1,fieldname2}. None of the other formats worked.
I agree. Terrible docs.
Here is how I filtered for only the email field returned:
?fields='members.email_address'
I'm using this instruction https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/advanced-integration/ to set up paypal. The only different is that I need to send data to CREATE_PAYMENT_URL endpoint, so I transfer it as second parameter
paypal.request.post(
CREATE_PAYMENT_URL,
JSON.stringify([{"name": "test", "price": 10}]),
{ headers: { 'Content-Type': 'application/json' } }
)
after that I'm getting 400 error
{"success":false,"errors":{"statuscode":400,"message":"Invalid json message received"}}
Backend doesn't matter, cause it's never reached. Also I'm almost sure it worked fine few days ago
Any ideas?
Fiddler screen
Try setting the second param to null, and send an options object as the third param with an object named json in the third with a plain JS object inside.
paypal.request.post(PAYMENT_URL, null, {
json : {
name:'test',
price:10
}
})
It's not in the documentation, I was having similar issues but dug around in the source code to get it to work.