wp-net / WordPressPCL

This is a portable library for consuimg the WordPress REST-API in (almost) any C# application
MIT License
342 stars 130 forks source link

Exception creating a Tag #112

Closed AntonStein closed 6 years ago

AntonStein commented 6 years ago

Always when i try to create a Tag i get an Exception "Du bist leider nicht berechtigt, die angegebenen Begriffe zuzuordnen." although the tag was created in wordpress. Is that a bug or am i doin something wrong?!

Tag curTag  = new Tag("name");
curTag.Description = "JobID";
Client.Tags.Create(curTag);

EDIT: I've just noticed, that the Exception is thrown when i try to create a post with the Tag I've created before.


Client.Posts.Create(CreateNewPost(job, curTag)).Wait();  //the exception is thrown here!

private static Post CreateNewPost(Job job, Tag tag)
{
            WordPressPCL.Models.Post _post = new Post()
            {
                Title = new Title(job.Offer.Title),
                Content = new Content(job.Offer.Html),
                Date = ParseDate(job.Offer.Publication.Start),
                Tags = new int[] { tag.Id }
            };
            return _post;
}

Any Idea what's wrong there?

with best regards

Anton

ThomasPe commented 6 years ago

Have you taken a look at the wiki here? https://github.com/wp-net/WordPressPCL/wiki/Working-with-Posts#create-new-post Have you authenticated the client?

I'll see if I can add a test to reproduce this.

AntonStein commented 6 years ago

Thanks for your answer. I've found my issue..

I was kinda unfocused, sorry :/...

Here is the solution:

Tag curTag  = new Tag("name");
curTag.Description = "JobID";
var createdTag = Client.Tags.Create(curTag).Result;

Client.Posts.Create(CreateNewPost(job, createdTag)).Wait(); 

private static Post CreateNewPost(Job job, Tag tag)
{
            WordPressPCL.Models.Post _post = new Post()
            {
                Title = new Title(job.Offer.Title),
                Content = new Content(job.Offer.Html),
                Date = ParseDate(job.Offer.Publication.Start),
                Tags = new int[] { tag.Id }
            };
            return _post;
}
PeterX29 commented 6 years ago

I'm trying to do a very similar code, to insert Tags. However I have a list with an array of tags. Like this:

 List<Tag> t = new List<Tag>();
                string sTag = textBoxTags.Text;
                // Split string on commas
                string[] words = sTag.Split(',');
                foreach (string word in words)
                {
                    t.Add(new Tag { Name = word.ToString(), Description = "Tag" });
                }

My issue is if the tag already exist give error. I already tried many approaches without success. Anyone can help, please?

Tag curTag = new Tag("name"); curTag.Description = "JobID"; var createdTag = Client.Tags.Create(curTag).Result;

Client.Posts.Create(CreateNewPost(job, createdTag)).Wait();

private static Post CreateNewPost(Job job, Tag tag) { WordPressPCL.Models.Post _post = new Post() { Title = new Title(job.Offer.Title), Content = new Content(job.Offer.Html), Date = ParseDate(job.Offer.Publication.Start), Tags = new int[] { tag.Id } }; return _post; }