Danbooru

New API questions

Posted under General

I tried switching to HttpClient, but trying to edit a post's tags still nets me 401 Unauthorized.

HttpClient client = new DefaultHttpClient();
HttpPut put = new HttpPut("http://danbooru.donmai.us/posts/"+id()+".xml");
put.getParams().setParameter("login", Danbooru.danbooruLogin);
//put.getParams().setParameter("password_hash", Danbooru.danbooruPassHash);
put.getParams().setParameter("api_key",Danbooru.apiKey);
put.getParams().setParameter("post[source]", source());
put.getParams().setParameter("post[tags]", tags());

try {
HttpResponse response = client.execute(put);
println( response.getStatusLine().toString() );
}...

I tried what you put before I started messing with HttpClient but figured I just wasn't doing it right when I got error code 500... I'm not able to see the server response the way I did it. And I don't know what I'm doing with HttpClient, so no surprise there when something goes wrong.

Updated

Something I notice about error json responses, the field names aren't wrapped inside double quotes, thus not being invalid json objects:

{success: false, message: "You cannot search for more than 2 tags at a time"}

should be:

{"success": false, "message": "You cannot search for more than 2 tags at a time"}

RaisingK said:

How do you get all of the comments associated with a specific post ID? I've tried different parameter variations to /comments.json, but I get the same main listing whatever I try.

help:api is of no help.

I'll go add it.

Edit: Fixed. You'll be able to use search[post_id]= after the change is live.

I dislike bothering with small details but, in the previous api (old danbooru) the access denied code was 403, now it is 401 and the wiki says that it is 403, it's a small thing but I'd like to know if it's supposed to be like that

Edit: oh there it is, thanks

Updated

Can someone else verify that comment deletion works through the API (when you have to authenticate, not when you can ride on your browser session)? I'd like to know that it's just me doing something wrong again.

I can't figure out how to provide credentials to a DELETE request in Java. Depending on which method I try, I get either 403 Forbidden or either of:

IOException: HTTP method DELETE doesn't support output

Exception in thread "main" java.lang.IllegalStateException: DELETE request cannot enclose an entity

Throwing my credentials in the URL gives me the 500 error I got all the other times I tried that.

Updated

^ are you still using HttpClient? i tried google and modified some code from stackoverflow and i arrived to something like this:

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.commons.codec.binary.*;

public class DanbooruCommentDelete {
    public static void main(String[] args) throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {

            final String DANBOORU_USER = "$login";
            final String DANBOORU_API_KEY = "$api_key";

            int commentID = $commentID;

            String encoding  = new String (Base64.encodeBase64 (StringUtils.getBytesUtf8(DANBOORU_USER + ":" + DANBOORU_API_KEY)));
            HttpDelete httpreq = new HttpDelete("http://danbooru.donmai.us/comments/" + commentID + ".xml");
            httpreq.setHeader("Authorization", "Basic " + encoding);
            System.out.println("executing request " + httpreq.getRequestLine());
            HttpResponse response = httpclient.execute(httpreq);
            HttpEntity entity = response.getEntity();

            System.out.println(response.getStatusLine());
            EntityUtils.consume(entity);
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
    }
}

please ignore the block below (1 line feed + 1 whitespace + 1 line feed):

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.commons.codec.binary.*;
 
public class DanbooruCommentDelete {
    public static void main(String[] args) throws Exception {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
 
            final String DANBOORU_USER = "$login";
            final String DANBOORU_API_KEY = "$api_key";
 
            int commentID = $commentID;
 
            String encoding  = new String (Base64.encodeBase64 (StringUtils.getBytesUtf8(DANBOORU_USER + ":" + DANBOORU_API_KEY)));
            HttpDelete httpreq = new HttpDelete("http://danbooru.donmai.us/comments/" + commentID + ".xml");
            httpreq.setHeader("Authorization", "Basic " + encoding);
            System.out.println("executing request " + httpreq.getRequestLine());
            HttpResponse response = httpclient.execute(httpreq);
            HttpEntity entity = response.getEntity();
 
            System.out.println(response.getStatusLine());
            EntityUtils.consume(entity);
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
    }
}

Updated

Awesome. That's what I needed.

String encoding  = new String (Base64.encodeBase64 (StringUtils.getBytesUtf8(Danbooru.danbooruLogin + ":" + Danbooru.apiKey)));
StatusLine status = Request.Delete("http://danbooru.donmai.us/comments/"+id()+".json")
	.addHeader("Authorization", "Basic " + encoding)
	.execute().returnResponse().getStatusLine();
println( status.getStatusCode() );

RaisingK said:

When I vote on a post through the API, the vote succeeds, but I get 500 for the status code. Is this a bug on my end or Danbooru's?

Status 500 is always "a error occurred", since the vote was registered successfully, i guess the error just occurs somewhere after the vote was registered in the database. I'd make a git issue about it with some more details.

Generally, i think status code 500 is always a bug in danbooru itself. At least, if there's not something like a typo in a critical parameter.

Hi everyone, I was wondering, is there a way to get the API key programaticaly? I'm still using the password_hash, that still works but as it is deprecated it may be removed in future releases (don't really know if it's true but just to be prepared...)

The only way to get the API key is to log in via web?

Thanks in advance.

What kind of script/program do you have?

  • If the user(s) using it are logged in, like for an userscript, no password hash or API key is needed, the API works via session as well.
  • If it's a personal program, you can copy/paste your API key into the script itself.
  • If it's a public program not in a browser (thus no session), then yea, i believe the only way to get it is to tell your users "log in & get the API key from your profile".
1 2