XenForo 2.0 API: Create New Thread as User ID

Anonimo88

Member
Registered
Joined
Apr 23, 2021
Messages
9
Points
3

Reputation:

Hello,
I would need to create threads automatically, I was able to do it through the API made available in XF 2.2, but this is automatically published with the admin user_id, while I would like to select another user..

I tried to add the user_id in the data sent via POST but this is ignored, I also tried to create an API in the name of the user I want to publish with, but when I enter his API Key he gives me this error:
User ID is not allowed to use this API key.

-- UPDATE --

I've try to login with auth/ and auth/login-token before creating the thread but it is always created by Admin
 
Last edited:

Soft4Win

Developer
Staff member
Moderator
Collaborate
Registered
Joined
Apr 27, 2019
Messages
216
Points
73

Reputation:

As far as i know, the auth route is only for validation of username and password.

In order to create thread from a user's account, you will have to create user specific api key. And make sure you have selected the scope for thread:write to make it work.
 

Anonimo88

Member
Registered
Joined
Apr 23, 2021
Messages
9
Points
3

Reputation:

Hello @Soft4Win and thanks for your reply,
I created the specific API key for the user from which I would like to publish the threads and I selected the "thread:write" item but if I try to publish the thread with the other API key it does not publish the thread and it returns me the error "User ID is not allowed to use this API key."
 

Soft4Win

Developer
Staff member
Moderator
Collaborate
Registered
Joined
Apr 27, 2019
Messages
216
Points
73

Reputation:

I see will test at my end and update you.
 

Soft4Win

Developer
Staff member
Moderator
Collaborate
Registered
Joined
Apr 27, 2019
Messages
216
Points
73

Reputation:

Anonimo88

Member
Registered
Joined
Apr 23, 2021
Messages
9
Points
3

Reputation:

Incredible..!

I use this code:
PHP:
<?php
//New Thread
$headers = array(
    'XF-Api-User: 1',
    'XF-Api-Key: bv6i7isL......'
);

$post = [
    'node_id'   =>  12,
    'title'     =>  'test',
    'message'   =>  'TestMessage'
];

$url = 'https://www.mywebsite.it/xenforo/api/threads/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
$result = json_decode($json);

echo print_r($result);

And this is the API key's settings:
image.png
 

Soft4Win

Developer
Staff member
Moderator
Collaborate
Registered
Joined
Apr 27, 2019
Messages
216
Points
73

Reputation:

Incredible..!

I use this code:
PHP:
<?php
//New Thread
$headers = array(
    'XF-Api-User: 1',
    'XF-Api-Key: bv6i7isL......'
);

$post = [
    'node_id'   =>  12,
    'title'     =>  'test',
    'message'   =>  'TestMessage'
];

$url = 'https://www.mywebsite.it/xenforo/api/threads/';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
curl_close($ch);
$result = json_decode($json);

echo print_r($result);

And this is the API key's settings:
image.png
Anonimo88Remove 'XF-Api-User: 1', from header and it should work.
 

Anonimo88

Member
Registered
Joined
Apr 23, 2021
Messages
9
Points
3

Reputation:

Incredible.. It work!
I did not understand that that command in the header referred to the user id.. Thanks friend and sorry for my ignorance!
 

Soft4Win

Developer
Staff member
Moderator
Collaborate
Registered
Joined
Apr 27, 2019
Messages
216
Points
73

Reputation:

Incredible.. It work!
I did not understand that that command in the header referred to the user id.. Thanks friend and sorry for my ignorance!
Anonimo88Glad it worked out. When using super key, you can set the XF-Api-User, and based on that user, all actions will be performed.
 
Top