I'm trying to add products using the WooCommerce .Net Rest API. I have managed to add a product in using this. However, I can't get it to add the product image.
I have the following code for adding a product:
Product p = new Product();
p.name = "Ben";
p.description = "Testing Dan's Example Code.";
p.short_description = "I hope it works.";
p.price = 3;
await wc.Product.Add(p);
I can't find anything on how to add an image to this product though.
p.images = ??;
I have found the following JSON which is used to add images but I can't work out the c# equivalent.
"images":[
{
"src":"https://www.example.com/image.jpg",
"position":0
}
],
Does anyone have any ideas regarding this?
EDIT: I have attempted to write my own way of uploading the image and have got the following:
List<Image> productImageList = new List<Image>();
productImageList.Add(new Image()
{
name = "TEST",
src = "www.test.com",
position = 0
});
However, image does not contain a definition for these names. Is there a WooCommerce Rest version of Image that would work?
EDIT2: To answer my own question above - Yes there is.
productImageList.Add(new ProductImage()
{
name = "TEST",
src = "https://res.cloudinary.com/pricecheck/image/upload/c_pad,h_800,w_800,d_noimg.jpg/TOAQU093-1.jpg",
position = 0
});
The above code will allow me to add a product to an image. However, the image doesn't retain its Cloudinary source once it has been uploaded. The image is added to the wordpress library and the source becomes this.
productImageList.Add(new ProductImage()
{
name = "TEST",
src = "https://res.cloudinary.com/pricecheck/image/upload/c_pad,h_800,w_800,d_noimg.jpg/TOAQU093-1.jpg",
position = 0
});
Prepare the Image JSON object as per the above. I have not more idea to how to create image JSON object.
Then, You can be passed the image object as below into the product object to create the product in WooCommerce.
Product p = new Product();
p.name = "Ben";
p.description = "Testing Dan's Example Code.";
p.short_description = "I hope it works.";
p.price = 3;
p.images = productImageList;
await wc.Product.Add(p);
Please Read the official docs of WooCommerce REST API Here
Related
I am building a google document from Google docs API.
My doc is created from a template: copy the template in new location, replace texts, and replace images.
Everything is OK until I try to replace my images.
The images to put in the file are stored in a Google Drive folder.
So, I have 2 questions:
the first one is how to use replaceImage request with viewing URL of my image?
By using following code, I am facing a 400 error.
IDictionary<string, InlineObject> inlineObjects = doc.InlineObjects;
//Get the objet ID
string imageObjectId = inlineObjects.First().Value.ObjectId;
BatchUpdateDocumentRequest batchUpdateRequest = new BatchUpdateDocumentRequest {
Requests = new List<Google.Apis.Docs.v1.Data.Request>()
};
var request = new Google.Apis.Docs.v1.Data.Request {
ReplaceImage = new ReplaceImageRequest() {
ImageObjectId = imageObjectId,
Uri = "https://docs.google.com/uc?export=view&id=1pmXP9TFolKMQoUlXHllvIrQPlZiDxId6"
}};
batchUpdateRequest.Requests.Add(request);
DocumentsResource.BatchUpdateRequest updateRequest =
_docSercive.Documents.BatchUpdate(batchUpdateRequest, fileId);
BatchUpdateDocumentResponse updateResponse = updateRequest.Execute();
Error:
Invalid requests[0].replaceImage: Access to the provided image was forbidden. [400]
Errors [
Message[Invalid requests[0].replaceImage: Access to the provided image was forbidden.] Location[ - ] Reason[badRequest] Domain[global]
]'
and the second one is, how to industrialize this by retriving image URL from Google Drive API?
When I try getting it right now, I can only access image name and extension:
Google.Apis.Drive.v3.Data.File f = _service.Files.Get(fileId).Execute();
am I supposed to use query params for this file.get call?
Thanks for your help!
I want to upload images to my Instagram account using an Api like InstaSharp or InstagramApiSharp on Github and C#. But have no idea how to do it.
There is UploadPhotoAsync() but can't specify what collection it should be uploaded to.
Anyone have any solutions?
You just pass InstaImage and String for caption.
InstaImage iamge = new InstaImage
{
URI = new Uri(Path.GetFullPath("image.jpg"), UriKind.Absolute).LocalPath,
Width = 720,
Height = 889,
};
IResult<InstaMedia> postResult = await _instaApi.UploadPhotoAsync(iamge,"caption");
I am looking to embed the following:
Using the Discord API. I have looked and the only resources I can find are for Python, Java, Ruby, etc.
But when using:
var embed = new Message.Embed(
{
Author =
{
Name = "Name",
Url = "www.url.com"
}
});
It comes back with the message:
And:
Not sure What I need to do to be able use the embed library. Just looking for some guidance on how this works
Edit:
When Using this I get no errors but when running the embed doesnt seem to build. It doesnt error. It just never builds the embed variable
var embed = new Message.Embed
{
Author =
{
Name = "Lawler",
Url = "www.twitch.tv/Lawler"
},
Title = "www.twitch.tv/Lawler",
Thumbnail =
{
ProxyUrl = "https://yt3.ggpht.com/-m-P7t2g-ecQ/AAAAAAAAAAI/AAAAAAAAAAA/YtS2YsD8-AM/s900-c-k-no-mo-rj-c0xffffff/photo.jpg",
Url = "www.twitch.tv/Lawler"
},
Description = "**Now Playing**\n" +
"Rocket League\n" +
"**Stream Title**\n" +
"Lawler RLCS Caster"
};
*Note: I am using Discord v 0.9.6
You can create an Embed Message like the following code (using the most recent version of Discord.Net):
var builder = new EmbedBuilder()
{
//Optional color
Color = Color.Green,
Description = "This is the description of the embed message"
};
Build a field inside the Embed Message:
builder.AddField(x =>
{
x.Name = Author.Name;
x.Value = Author.Url;
x.IsInline = false;
});
And reply to the same channel context:
//Use await if you're using an async Task to be completed.
await ReplyAsync("", false, builder.Build())
The code above should build an embed message, there are more options in the Discord.Net docs. Link: https://docs.stillu.cc/guides/introduction/intro.html
I hope you find this helpful.
Just a quick look at your code, I think you've got a close parenthesis in the wrong place.
Try the following:
var embed = new Message.Embed()
{
Author =
{
Name = "Name",
Url = "www.url.com"
}
};
Again, without doing any research you may also need to do the following:
var embed = new Message.Embed()
{
Author = new Author()
{
Name = "Name",
Url = "www.url.com"
}
};
var embed = new EmbedBuilder()
instead of
var embed = new Message.Embed()
To send the message:
await Context.Channel.SendMessageAsync("", false, embed);
EDIT: 0.9.6 doesn't support embeds, so the code above is useless
If you are in Discord.Net 1.0.1, you can format an embed like so:
var eb = new EmbedBuilder() { Title = "Cool Title", Description = "Description" };
Read the documentation here for more info here.
And if you want to make your text look a little better, you can read the Discord Markdown Documentation here. This works in 0.9.6.
To send an embed use:
await Context.Channel.SendMessageAsync("", false, eb);
I newbie using Facebook SDK for C#
I have seen this post to wall in unit test.
Get Access Token programmatically in Unit Test Method
Now, I want to delete post in my wall.
using Facebook;
[TestMethod]
public void Post_to_the_wall()
{
var client = new FacebookClient(token);
dynamic parameters = new ExpandoObject();
parameters.message = "Check out this funny article";
parameters.link = "http://www.example.com/article.html";
parameters.picture = "http://www.example.com/article-thumbnail.jpg";
parameters.name = "Article Title";
parameters.caption = "Caption for the link";
parameters.description = "Longer description of the link";
parameters.actions = new
{
name = "View on Zombo",
link = "http://www.zombo.com",
};
parameters.privacy = new
{
value = "ALL_FRIENDS",
};
dynamic result = client.Post("me/feed", parameters);
// TODO: NOW, delete the post ???
}
Any suggestions ?
Simply do this:
dynamic result = client.Post("me/feed", parameters);
client.Delete(result.id);
Post method get Postid (id property), and you can use it for delete the post.
https://developers.facebook.com/docs/graph-api/reference/v2.2/post
An app can delete a post if it published it
Check out the example code on that page. You need a User Token with publish_actions to make a DELETE request to /post-id.
I am using JoeBlogs https://github.com/alexjamesbrown/JoeBlogs to handle stuff on some of my wordpress websites. I am having some problems with creating a new category and uploading a picture.
Here is my code for creating a new category:
var wpWrapper = new WordPressWrapper("http://192.168.1.2/xmlrpc.php", "admin", "admin");
wpWrapper.NewCategory("some description", 0, "cat1", "slug here");
I am getting the following error from CookComputing library:
XmlRpcServerException: Not Found
I get the same error for uploading a picture. I've tried 2 versions of uploading a picture with uploadfile and newmediaobject.
Here is what I've made with the newmediaobject:
var blog = new WordPressWrapper("http://192.168.1.2/wordpress", "admin", "admin");
byte[] imageData = System.IO.File.ReadAllBytes("desert.jpg");
var img = blog.NewMediaObject(new MediaObject { Bits = imageData, Name = "desert.jpg", Type = "image/jpeg" });
I am getting this error: Response from server does not contain valid XML
Here is my second try with uploadfile:
var blog = new WordPressWrapper("http://192.168.1.2/wordpress", "admin", "admin");
wpWrapper.UploadFile("desert.jpg", "desert.jpg", true,"image/jpeg");
I am getting the same error, with the invalid XML (just like the first try with mediaobject).
What do you guys suggest ?
I am open to other libraries that could help me achieve this.
Ok, so the problems look like they were REALLY SIMPLE.
The xmlrpc.php file for my wordpress is in this path
192.162.1.2/wordpress/xmlrpc.php
In the first part (create category), I forgot to add "wordpress" in the uri.
For the second part (upload image), I haven't forgot to add "wordpress" but I forgot to add "xmlrpc.php" at the end.
Stupid mistakes, but for those who encouter the same problems, check that out first.