Jump to content

[Solved] A question about backpack tf api (recreated)


Not_a_good_sniper

Recommended Posts

I creater this new topic bc other topic was created in the wrong forum.

 

Hi!

 

I'm new to this (I mean sending a request to the server)

 

I want to find a way to use this backpack tf api and create and delete listings automatically. The only thing I did was create a script that can accept trade offers (or reject), send messages to users and do something with tf2 Inventory (sort by type and craft/smelt metal), but I have to create and delete listings myself, so I really want to know how to use the backpack tf api.

 

I found so many topics about backpack tf , but they were old, and nothing works for me 😕. Maybe I dont understand how properly send a request to a backpack tf.

Sorry if I will make you angry bc the code bellow is bad. I am comepletely new to this, but a little help will make me happy(if it works of course, but any suggestion is also good)

const https = require('https');

const dataToSend = JSON.stringify({
	'token': 'my token', // this token is from https://backpack.tf/connections
	'listings_id': [
		'6856352614'// ID of the item which I found in the url link of its history
	]
});

const options = {
	hostname: 'backpack.tf',
	port: '80',
	path: '/api/classifieds/delete/v1', // The problem will not be solved if I will use /api/1.0/classfields/delete path.
	method: 'DELETE',
	headers: {
		'Content-Type': 'application/json', // If I will add an 'Authorization' header with my token, nothing happens
	}
};

const req = https.request(options, (res) => {
	console.log('Status: ${res.statusCode}');
	console.log('Headers: ${JSON.stringify(res.headers)}');
	res.SetEncoding('utf8'); // Dont know what this thing is doing, but as I understand its translating a response to an other language i guess? Anyways, removing this wont help

	res.on('data', (chunk) => {
		console.log('Body: ${chunk}');
	});

	res.on('end', () => {
		console.log('End of the response / no more data will be recieved');
	});
});

req.on('error', (err) => {
	console.log('An error with the request: '+err);
});

req.write(dataToSend);
req.end();

I was reading a few guides and stole a code According to some guides, this is the common code using 'https module'

 

I was typing random words, and this is the final code without any syntax error or something. If i change a port or hostname there will be no error message, and i think the syntax of this code is alright.

 

In this code I want to learn how to properly send a request and as a test I want to delete one of my listing.

 

When I am launching this script I am getting this message <An error with the request: Error: write EPROTO 15580:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:\ws\deps\openssl\openssl\ssl\record\ssl3_record.c:332:>

 

The only thing i understand from this error message is <wrong version number>. I have googled the EPROTO and the only useful information I found is "node.js version is old or you need an older one".

Older? Really, is this a solution for my problem? (I dont tested it yet, but I can try. I dont like to reinstall lots of apps and clean my pc after them, but if you think this is a problem, i can try)

My current node.js version 12.18.2

 

Guys, please, I really want to know how to do this. I spent a few days trying to do sometihng, but this is the only code I can make for sending a request to a server.

 

Thank you in advance.

Link to comment
Share on other sites

22 minutes ago, Vortegan said:

Nicklason, the creator of tf2-automatic, has created a npm module called node-bptf-listings

 

You can install it like so (make sure you enter this command in the directory of the project):


npm install tf2-schema bptf-listings

To get started, he has got some nice examples: SetupAdd and Remove Listings.

Ty I will try it later.

 

But if someone know how to send a request with https module then pls help me. I want to use https more than this module

 

Edit: I want to learn to code something, not to use someones work. I want to do everything myself

Link to comment
Share on other sites

I would at the very least recommend you to use modules like axios or request to send your requests to the server.

 

The http/s modules, while they can do everything, are a bit clumsy to use. The modules I mentioned above can add a lot of sugar to make it easier to digest: A lot of common cases can be handled with just a tiny bit of code etc. 

 

Regardless of what you decide to use, If you have a look at how the node-bptf-listings module removes listings, it sends a DELETE request to https://backpack.tf/api/classifieds/delete/v1 with a query string of "token" as your token and a body of "listing_ids" as an array of the id's of the listings you want to remove. 

Link to comment
Share on other sites

28 minutes ago, Vortegan said:

it sends a DELETE request to https://backpack.tf/api/classifieds/delete/v1

Is my code not doing the same request as you said?

28 minutes ago, Vortegan said:

As far as I understand, this code uses the request module. Is it really better to use someone else's module than a built-in one? Anyway, I'll try to find out how to use the request module as it has been mentioned in some tutorials. But still, I prefer to use a built-in one.

 

Nvm, I cant, I will try to use request module

Link to comment
Share on other sites

6 minutes ago, Not_a_good_sniper said:

Is my code not doing the same request as you said?

No, like I said, "with a query string of "token" as your token and a body of "listing_ids" as an array of the id's of the listings you want to remove", you are putting the token in the body.

 

8 minutes ago, Not_a_good_sniper said:

Is it really better to use someone else's module than a built-in one?

I'm not really sure which article you read that said https is the best module to use, but the above request looks like this using the request modules: 

const request = require('request');

const url = "https://backpack.tf/api/classifieds/delete/v1";
const token = "YOUR_TOKEN";
const listingsToDelete = ["id1", "id2"];

const options = {
    method: 'DELETE',
    url: url,
    qs: {
        token: token
    },
    body: {
        listing_ids: listingsToDelete
    },
    json: true,
    gzip: true
}

request(options, (err, response, body) => {
    if (err) {
        return console.log("There was an error deleting the listings.");
    }

    // Done.
})


As you can see, it is much more concise and simpler. 

Link to comment
Share on other sites

5 minutes ago, Vortegan said:

As you can see, it is much more concise and simpler. 

Yes, maybe. I was trying to read a code which you sent before, its so hard to understand. But this code looks really simple. Thanks for an example, I am playing with it right now.

 

8 minutes ago, Vortegan said:

I'm not really sure which article you read that said https is the best module to use

I mean original = almost better than others, for example there is a modification for the game not made by a game developers. Yeah, community can create something interesting, but game developers can do it much better since they know everything about their game, they know how to better add this modification to the game without perfomance looses and better interaction with other scripts. Thats just my opinion, no one says http/s is the best, i just prefer something original.

Link to comment
Share on other sites

Request is deprecated, you can of course still use it but i highly recommend axios as you want to make the requests yourself. (or keep using https ig)

listing_ids is not an assetid, like 6856352614 in your example. For sell orders i believe its 440_<assetid>, and buy orders its 440_<steamid>_<listinghash> . You can get your own listings from /api/classifieds/listings/v1 with your user token

 

Link to comment
Share on other sites

32 minutes ago, Zeus_Junior said:

Request is deprecated, you can of course still use it but i highly recommend axios as you want to make the requests yourself. (or keep using https ig)

listing_ids is not an assetid, like 6856352614 in your example. For sell orders i believe its 440_<assetid>, and buy orders its 440_<steamid>_<listinghash> . You can get your own listings from /api/classifieds/listings/v1 with your user token

 

Ok I found an item id if i will just go to the https://backpack.tf/api/classifieds/listings/v1 link, and yes it looks like 440_<id>

 

But with axios there is another error. Here is a code (copyied from https://github.com/axios/axios)

const axios = require('axios');

axios.get('backpack.tf/api/classifieds/listings/v1', {
	params: {
	token: 'token' 
	}
})
.then(function (response) {
	console.log(response);
})
.catch(function (error) {
	console.log(error);
})
.then(function () {
	// always executed
}); 

I am assuming its catching an error and printing out an error with ECONNREFUSED.  Dont know what i am missing, maybe there is a request that i have to make before using any others, or i have to be premimum. But what i am missing? Is it something wrong here? You can ignore it and i will understand, but really, what wrong? Everything here is from github tutorial and a token as you said.

 

Edit: i have made a new code, still with error, but i can find a message with <This access token is not valid> with status code 401. What does <user session is invalid> mean?

const axios = require('axios');

axios.get('https://backpack.tf/api/classifieds/listings/v1', {
	header: {
		Authorization: 'token' 
	}
})
.then(function (response) {
	console.log(response);
})
.catch(function (error) {
	console.log(error);
})
.then(function () {
	// always executed
}); 

I think i am soo close, but something is stopping me. You of course can ignore it, but i think i am very close.

Link to comment
Share on other sites

You are doing a GET request with axios.get, use axios.delete for the DELETE request. And you are trying to use the Auth header for the token, instead you should pass it as a param/query string. 

const axios = require('axios');

const url = "https://backpack.tf/api/classifieds/delete/v1";
const token = "YOUR_TOKEN";
const listingsToDelete = ["id1", "id2"];

const options = {
  data: { listing_ids: listingsToDelete },
  params: { token: token }
};

axios.delete(url, options)
.then(res => {
  console.log(res);
})
.catch(err => {
  console.log(err);
})
.then(() => {
  // Always Executed
});

 

Link to comment
Share on other sites

9 hours ago, Vortegan said:

You are doing a GET request with axios.get

I know, look at the url. I dont think its deleting listings.

Link to comment
Share on other sites

So i made a GET request code to get listings without errors, but it responses with [ogject Ojcet].

 

I tried to make a response.listings or response["listings], but nothing happened. Is it ok to get a response like this?

 

const axios = require('axios');

const url = "https://backpack.tf/api/classifieds/listings/v1";
const token = "my token";

const options = {
  params: { token: token }
};

axios.get(url, options)
.then(res => {
  console.log("Response: "+res);
})
.catch(err => {
  console.log("Error :"+err);
})
.then(() => {
  // Always Executed
});

 

Link to comment
Share on other sites

^ that is if you use

console.log("Response: "+res);

if you just do 

console.log(res);

or

console.log("Response:", res);

it looks fine

Link to comment
Share on other sites

On 9/6/2020 at 7:45 PM, Zeus_Junior said:

^ that is if you use


console.log("Response: "+res);

if you just do 


console.log(res);

or


console.log("Response:", res);

it looks fine

Yes thanks, now i can see lots of stuff but no listings.

 

Edit: nvm I can see them now. Thank you really much

Link to comment
Share on other sites

Hello again. I found something strange about the axios module. Some resources say “you can send a DELETE request with data”, others “you cannot do this, axois cannot send a DELETE request with data”.

 

I was using this topic and this one to find out how to send a data with a DELETE request. But it turned out that there are 0 topics in which the author is using the axios module. I even used the code as an example that Zeus_Junior sent me recently (but I need params, not a qs).

 

I know what are you about to say. I know those author are using request module, but I want to find how to correctly send a data (I mean to format the data correctly to send the request).

 

const url = "https://backpack.tf/api/classifieds/delete/v1";
const token = "user_token";
const listingsToDelete = ["730_19420736526", "440_9197937815"]; // those id's are from the GET listings request

const options = {
  headers : {"Content-Type": "application/json"}, // dont know should this header be here but i added it
  params: {token: token},
  data: {"listing_ids": JSON.stringify(listingsToDelete)} // tried to use '{"listing_ids": ["730_19420736526", "440_9197937815"]}'. Nothing happens.
};

axios.delete(url, options)
.then(res => {
  console.log("Response: ", res);
})
.catch(err => {
  console.log("Error :"+err);
})
.then(() => {
  // Always Executed
});
Response: data: { deleted: 0, errors: [] }

According to this forum, I made the code correctly, but I am not sure if it should works for me.

 

Also as I understand I can create a config for axios, but I am always thinking about  Some resources say “you cannot do this". If its true then configs will not help me.

And someone's also got this problem but it was solved by adding a header.

 

I am assuming I cannot send data to the server, I am also including the fact that I might be missing something obvious.

Link to comment
Share on other sites

I think I found a solution for my problem. All I have to do is make a request to this url https://backpack.tf/api/classifieds/delete/v1?=user_token and just write data with listing_ids: [“id1”, “id2”,”ect”].

 

I cant check if it works but I have not tried this and I don’t have an access to my computer right now. But it looks like good, and I believe this will work.

Link to comment
Share on other sites

31 minutes ago, Zeus_Junior said:

Isn’t that what you did?

I mean include the token in the request url, write the data to the options, and remove the params.  If I do this I think it will send data.  I have not tried to send a request like that (as I said. I thought, why should I do this).  I do not get the error "this list does not exist".  And I think that if you send a request t o this link, axois will send the data. Axios is not sending a data for some reason. And I don’t know why

Link to comment
Share on other sites

Is this what I don’t need? Just copy this, paste and ready. It looks very simple, but I know how I am “copying the code”. Even here I will get an error, because “I know how to send a request”.

Link to comment
Share on other sites

const axios = require('axios')

axios({
	url: 'https://backpack.tf/api/classifieds/delete/v1',
	method: 'DELETE',
	params: {
		token: 'token'
	},
	data: {
		listing_ids: ['440_76561198144346135_2c6ff8fda5c837df836b66de9fa0b69f', '440_9084499509']
	}
}).then(({ data }) => {
	console.log(data);
})

// { deleted: 2, errors: [] }

Use this instead of axios.delete

 

Link to comment
Share on other sites

15 minutes ago, Zeus_Junior said:

const axios = require('axios')

axios({
	url: 'https://backpack.tf/api/classifieds/delete/v1',
	method: 'DELETE',
	params: {
		token: 'token'
	},
	data: {
		listing_ids: ['440_76561198144346135_2c6ff8fda5c837df836b66de9fa0b69f', '440_9084499509']
	}
}).then(({ data }) => {
	console.log(data);
})

// { deleted: 2, errors: [] }

Use this instead of axios.delete

 

I'll try your code later, but it's weird if this kind of code format works but axios.delete doesn't.  Thanks anyway.

Link to comment
Share on other sites

Following the docs you'd expect simply changing to this

const axios = require('axios')

axios.delete('https://backpack.tf/api/classifieds/delete/v1', {
	params: {
		token: 'token'
	},
	data: {
		listing_ids: ['440_76561198144346135_2c6ff8fda5c837df836b66de9fa0b69f', '440_9084499509']
	}
}).then(({ data }) => {
	console.log(data);
})

// { deleted: 0, errors: [] }

to work. But it doesn't

Link to comment
Share on other sites

36 minutes ago, Zeus_Junior said:

Following the docs you'd expect simply changing to this


const axios = require('axios')

axios.delete('https://backpack.tf/api/classifieds/delete/v1', {
	params: {
		token: 'token'
	},
	data: {
		listing_ids: ['440_76561198144346135_2c6ff8fda5c837df836b66de9fa0b69f', '440_9084499509']
	}
}).then(({ data }) => {
	console.log(data);
})

// { deleted: 0, errors: [] }

to work. But it doesn't

Ok

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...