XF 2.1 question about XenForo API

AhmedAbdallah

Active member
goodmorning friends
todays i need your help
so, i have an xenforo forums and i had created an api super user.
what i need from you is :
  1. how to close any thread using the api, i find the edit thread but no option to close it.
  2. how to create an new thread using the super user, i mean how to create thread using any user name or id, because i want to make an desktop app, so the user will able to login in the forums and post thread without going to forums web site.
  3. my forums is an arabic forums, so, when i use the get thread api, it give me the arabic text as \0...., i mean no arabic text just numbers. can any one show me how to solve this problem please?
i hope any one can help me.
thanks in advance
 

JulianD

Well-known member
goodmorning friends
todays i need your help
so, i have an xenforo forums and i had created an api super user.
what i need from you is :
  1. how to close any thread using the api, i find the edit thread but no option to close it.
  2. how to create an new thread using the super user, i mean how to create thread using any user name or id, because i want to make an desktop app, so the user will able to login in the forums and post thread without going to forums web site.
  3. my forums is an arabic forums, so, when i use the get thread api, it give me the arabic text as \0...., i mean no arabic text just numbers. can any one show me how to solve this problem please?
i hope any one can help me.
thanks in advance
I haven tried it but according to the documentation you should be able to close a thread by editing it and setting the discussion_open parameter to false.

There is a great resource by @Sim that explains how to use the API in case you need help with that:


Regarding the usage of the API as another user, this is something that is currently missing from the API, there is no support for OAuth login or user/password login (with a regular, no super user API). You can use a Super User API Key in case you're integrating with another closed platform. But if you plan to create a mobile application, it wouldn't be advisable to use a super user API key because you would have to include it in your mobile application and anyone with enough free time would be able to get it.

My advise is to use the BD API (https://github.com/xfrocks/bdApi/tree/xenforo2 currently in Beta) or to implement a plugin to authenticate the user , create a user API key for that user and pass that API key to your application. That is the only solution right now.

regarding the arabic text, that seems to be an encoded string, try to unencode it with a function such as utf8_decode or if you think it's a bug, open a bug report.
 

Sim

Well-known member
how to create an new thread using the super user, i mean how to create thread using any user name or id, because i want to make an desktop app, so the user will able to login in the forums and post thread without going to forums web site.
Right now you would need to manually create a user-specific api key and give that to the user. There is no mechanism for users to generate their own keys.

So if there are hundreds or thousands of users who will be using the desktop app - it's not currently practical to use the API, since you'd need to manually create all of those API keys.

I would hope that this changes in the future and permitted users can generate their own API key (with restrictions imposed by the administrators of course!).
 

AhmedAbdallah

Active member
I haven tried it but according to the documentation you should be able to close a thread by editing it and setting the discussion_open parameter to false.

There is a great resource by @Sim that explains how to use the API in case you need help with that:


Regarding the usage of the API as another user, this is something that is currently missing from the API, there is no support for OAuth login or user/password login (with a regular, no super user API). You can use a Super User API Key in case you're integrating with another closed platform. But if you plan to create a mobile application, it wouldn't be advisable to use a super user API key because you would have to include it in your mobile application and anyone with enough free time would be able to get it.

My advise is to use the BD API (https://github.com/xfrocks/bdApi/tree/xenforo2 currently in Beta) or to implement a plugin to authenticate the user , create a user API key for that user and pass that API key to your application. That is the only solution right now.

regarding the arabic text, that seems to be an encoded string, try to unencode it with a function such as utf8_decode or if you think it's a bug, open a bug report.
thank you to your interisting to my topic, please is there any way available to make an ad-don that can make an api keys to all users, because i want to use an ad-don to create an api keys to all users and put it in an xml file that i can link it with my application to get the users api keys directly from the xml file.
 

AhmedAbdallah

Active member
Right now you would need to manually create a user-specific api key and give that to the user. There is no mechanism for users to generate their own keys.

So if there are hundreds or thousands of users who will be using the desktop app - it's not currently practical to use the API, since you'd need to manually create all of those API keys.

I would hope that this changes in the future and permitted users can generate their own API key (with restrictions imposed by the administrators of course!).
hello sir, thanks to your reply, we also hope that, but we must find a way that we can use it right now, because our forums must be more accessible with screen readers for blind users, unfortunately the xf 2 is less accessible
 

AhmedAbdallah

Active member
when we try to get a title of arabic thread using the api we got this result
Code:
\u0642\u0633\u0645 \u0642\u0627\u0631\u0626\u0627\u062a \u0627\u0644\u0634\u0627\u0634\u0629
we are using the WinHttp library, also we try to use encod utf8_decode function But it did not work
 

AhmedAbdallah

Active member
@Mike @Chris D
hello, i've some problems with your script api.
so, when i tried to use the get threads option with arabic forum i got a coded text e.g
JSON:
{
    "thread": {
        "can_edit": false,
        "can_edit_tags": false,
        "can_hard_delete": false,
        "can_reply": false,
        "can_soft_delete": false,
        "can_view_attachments": false,
        "custom_fields": {},
        "discussion_open": true,
        "discussion_state": "visible",
        "discussion_type": "",
        "first_post_id": 1,
        "first_post_reaction_score": 0,
        "Forum": {
            "breadcrumbs": [
                {
                    "node_id": 1,
                    "title": "Main category",
                    "node_type_id": "Category"
                }
            ],
            "description": "",
            "display_in_list": true,
            "display_order": 1,
            "node_id": 2,
            "node_name": null,
            "node_type_id": "Forum",
            "parent_node_id": 1,
            "title": "Main forum",
            "type_data": {
                "allow_poll": true,
                "allow_posting": true,
                "can_create_thread": false,
                "can_upload_attachment": false,
                "discussion_count": 2,
                "last_post_date": 1593187372,
                "last_post_id": 2,
                "last_post_username": "MesterPerfect",
                "last_thread_id": 2,
                "last_thread_prefix_id": 0,
                "last_thread_title": "test test test",
                "message_count": 2,
                "min_tags": 0,
                "require_prefix": false
            }
        },
        "last_post_date": 1593186500,
        "last_post_id": 1,
        "last_post_user_id": 1,
        "last_post_username": "MesterPerfect",
        "node_id": 2,
        "post_date": 1593186500,
        "prefix_id": 0,
        "reply_count": 0,
        "sticky": false,
        "tags": [],
        "thread_id": 1,
        "title": "\u0647\u0630\u0627 \u0645\u0648\u0636\u0648\u0639 \u062a\u062c\u0631\u064a\u0628\u064a",
        "User": {
            "avatar_urls": {
                "o": null,
                "h": null,
                "l": null,
                "m": null,
                "s": null
            },
            "can_ban": false,
            "can_converse": false,
            "can_edit": false,
            "can_follow": false,
            "can_ignore": false,
            "can_post_profile": false,
            "can_view_profile": true,
            "can_view_profile_posts": true,
            "can_warn": false,
            "custom_fields": {
                "skype": null,
                "facebook": null,
                "twitter": null
            },
            "is_staff": true,
            "last_activity": 1593187372,
            "location": "",
            "message_count": 2,
            "reaction_score": 0,
            "register_date": 1593184514,
            "signature": "",
            "trophy_points": 0,
            "user_id": 1,
            "user_title": "Administrator",
            "username": "MesterPerfect",
            "website": ""
        },
        "user_id": 1,
        "username": "MesterPerfect",
        "view_count": 0
    }
}
but when i tried the same option with an english forum i got the correct text.
note: my DB is in utf-8 format
do you have a solution to my problem.
so i tried this using WinHTTP library, with vbs script, also with the autoit language, i used also php.
but the same problem.
hai, am waiting for your replies.
thanks in advance
 

Mike

XenForo developer
Staff member
The \uXXXX escape sequences are escaped Unicode characters. This is a standard feature of JSON and actually PHP's default (we don't override it). Your JSON decoding library should handle decoding this back to UTF-8/your character set automatically.
 

AhmedAbdallah

Active member
The \uXXXX escape sequences are escaped Unicode characters. This is a standard feature of JSON and actually PHP's default (we don't override it). Your JSON decoding library should handle decoding this back to UTF-8/your character set automatically.
so, i will test json and i will give you the result.
please i've an other q
can i post threads using api super user?
 

AhmedAbdallah

Active member
If you use a super user key, you can define the user that the action is taken by. This is covered here:

hi again
first, thank you mr @Mike
what you gave us work 100%

i have an other question now. and i hope you can help me
i tried to create a new conversation using the api
their are a param named:
recipient_ids
what i need to put their
when i tried to put
the user id e.g
2
it told me error
and i tried to put also
1,5,9
the same msg error
 

Mike

XenForo developer
Staff member
Anything that expects an array (usually when it has a type description similar to int[]) would be passed in like recipient_ids[]=1. For multiple entries, you'd repeat that. Something like recipient_ids[]=1&recipient_ids[]=5.

Decent HTTP libraries generally tend to build these requests for you when you pass in something like 'recipient_ids' => [1, 5].
 

AhmedAbdallah

Active member
Anything that expects an array (usually when it has a type description similar to int[]) would be passed in like recipient_ids[]=1. For multiple entries, you'd repeat that. Something like recipient_ids[]=1&recipient_ids[]=5.

Decent HTTP libraries generally tend to build these requests for you when you pass in something like 'recipient_ids' => [1, 5].
Right now you would need to manually create a user-specific api key and give that to the user. There is no mechanism for users to generate their own keys.

So if there are hundreds or thousands of users who will be using the desktop app - it's not currently practical to use the API, since you'd need to manually create all of those API keys.

I would hope that this changes in the future and permitted users can generate their own API key (with restrictions imposed by the administrators of course!).
hai, today with new problem in the api
why the delete commands not working?
i tried it with winhttp, with php, and also with cpp.
the result always is null
here is what i tried
PHP:
<?php

$curl = curl_init();

curl_setopt_array(
    $curl, [
        CURLOPT_URL => "http://myforums.com/api/users/1742/",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_TIMEOUT => 0,
        CURLOPT_CUSTOMREQUEST => "DELETE",
        CURLOPT_HTTPHEADER => [
            "XF-Api-Key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        ],
    ]
);

$response = curl_exec($curl);

curl_close($curl);

$data = json_decode($response, true);

var_dump($data);
note the example is made by @Sim
 

AhmedAbdallah

Active member
Check the response code returned - https://www.php.net/manual/en/function.curl-getinfo.php

Does you API user have the correct permissions to delete a user? (should return 403 if not)

Does the user exist? (should return 404 if not)
@Sim
we tried what you told us but we got the 403 return
the problem always the same
i tried to pass an api-key for a super user, also i tried to pass an api-key for a admin user that have all permitions.
can you give us an example please?
PHP:
<?php

$curl = curl_init();

curl_setopt_array(
    $curl, [
        CURLOPT_URL => "http://myforums.com/api/users/1742/",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_TIMEOUT => 0,
        CURLOPT_CUSTOMREQUEST => "DELETE",
        CURLOPT_HTTPHEADER => [
    "XF-Api-Key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "XF-Api-User: 2"
        ],
    ]
);

$response = curl_exec($curl);
$info = curl_getinfo ($curl);
curl_close($curl);

$data = json_decode($response, true);

echo $info['http_code'];
 

AhmedAbdallah

Active member
@Mike
inadition to the delete command
i have also a question about the custom fealds
how i can use it
e.g
when creating user
i want to give the cusstom fealds
windows
screenReader
phoneNumber
the
screen reader and the windows is a chois
1=xp
2=windows_7
3=windows_8
4=windows_8.1
5=windows_10

1=Jaws
2=NVDA
3=none
the Phone number is a Input
can any one give me a small example about how to use this?
 

Sim

Well-known member
To delete a user, your API user must have both the user:delete scope:

1593379438475.png

... and be an administrator with the Manage users and moderators permission:

1593379488705.png
 

AhmedAbdallah

Active member
@Chris D
@Mike
good morning
I'm sorry to bother you.
i realy need your help
i started programing my app
i already do the login function
i need to know how to yous the custom fealds to make the register function
inadition to the delete commands
also i have a small question
when i use the api to register a new account is the Forum send the e-mail activation to users
 
Top