At the Moment, my Task is to automate some processes in our Company. One of them is to automaticilly creating pages on our Knowledgebase from Attlassian Confluence.
Normally I'm a .Net developer and I'm not familiar with REST API's. So I've read the documentation and that gave me the following line to create a new page:
curl -u admin:admin -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"new page","space":{"key":"TST"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' http://localhost:8080/confluence/rest/api/content/
My Problem is, that I don't know, how to do this in C#.
I don't have a code example because I've done this via PowerShell and the ConfluencePS module. However, much of it can be done leveraging the .Net Framework.
In case it's useful to you, I suggest you familiarize yourself with the following - some of it depending on your .Net Framework version (e.g., 4.5+):
Basic Authentication (and Base64 encoding).
JavaScript Object Notation (JSON).
System.Net.Http.Httpclient.
System.Net.NetworkCredential.
System.Net.Http.HttpClientHandler.
And, a word of warning! When dealing with the Confluence RESTful API documentation make certain the documentation is for your specific Confluence version. Sounds obvious but it can be difficult to line the two up correctly.
Related
I googled now for almost an hour and can't find anything that would help me out. I'm a beginner programer and took the course over from TeamTreeHouse with Serialization in C#. Here I also learned how to use the WebClient.
Now I thought a good practice Project would be to make a Translate app that sends to google the user input in a POST Request and Google returns the answer as Json which I deserialize.
Problem is I read through the documentation of the api but I'm so confused of what I should send exactly to google and really how to do this?
I know the method webclient.Headers.Add(arguments here) , but I really don't know what else it needs.
You can find an example over here:
https://cloud.google.com/translate/docs/translating-text
https://translation.googleapis.com/language/translate/v2 Three query
parameters are required with each translation request:
Target language: Use the target parameter to specify the language you
want to translate into. Source text string: Use the q parameter to
specify each text string to translate. API key: Use the key parameter
to identify your application. If you are using OAuth 2.0 service
account credentials (recommended), do not supply this parameter.
So problem is there is no placeholder in that example URL where I could put my api key + soure text string an Target language.
So what exactly should I send to google so it knows what I want and returns me the JSON file?
Maybe anyone could help me out. And I know there is an official Library for exactly this but I want to practice serialization and using web scraping with the WebClient class so I want to do it like this.
You specifically asked about a POST.
You can use this url:
https://translation.googleapis.com/language/translate/v2?key=MY_KEY
Of course, replace MY_KEY with your key.
Add a header for the content type:
application/json; charset=utf-8
Format your text and target language as JSON and write it to your request stream:
{"q":"Team work is a major progress maker at this location, it appears that everyone is willing to help when they can.","target":"fr"}
The documentation here: https://cloud.google.com/translate/docs/reference/translate#body.QUERY_PARAMETERS
says use query parameters q, target, key, etc.
So your URL should be something like https://translation.googleapis.com/language/translate/v2?key=[yourAPIkey]&target=language
I'm thinking of porting a JavaScript web app to C# Unity3D (Free / Personal Version) for an RPG I'm developing. I have an extensible, separate API built in PHP Laravel 5.1, which my game interacts with through jQuery http calls.
I need to continue making standard restful calls, get, post, put, delete, etc within Unity but have only found UnityEngine.WWW# which makes gets and posts.
This SO Post shares the other available Unity3D http methods, but none which actually get all restful calls in one. I'm asking again because this was posted in 2012 and I haven't found any updates which satisfy this within the updated documentation.
There is Best HTTP Basic and Best HTTP for $45 and $55, but was thinking there would be other free options.
Am I missing something within Unity that allows for standard restful calls?
WebClient and WebRequest are both available in Unity and looks like it will only work with Pro Unity version just like any other API from the System.Net namespace. I don't know if this restriction has changed in Unity 5. They support all those restful calls mentioned in your question.
Unity Added a new API called UnityWebRequest in version 5.2 with mobile platform support in 5.3. It was designed to replace WWW and it supports all the restfull calls listed in your question. Below are example for each one. This is not a full example. You can find full examples in the link I provided above.
//Get
UnityWebRequest get = UnityWebRequest.Get("http://www.myserver.com/foo.txt");
//Post
UnityWebRequest post = UnityWebRequest.Post("http://www.myserver.com/foo.txt","Hello");
//Put
byte[] myData = System.Text.Encoding.UTF8.GetBytes("This is some test data");
UnityWebRequest put = UnityWebRequest.Put("http://www.my-server.com/upload", myData);
//Delete
UnityWebRequest delete = UnityWebRequest.Delete("http://www.myserver.com/foo.txt");
You can see complete example for each one including posting with json here.
I'm rather new to sending/receiving over networks/sockets/network streams and so on.
I'm making an IRC program that can communicate with Twitch.tv. They have an API, and they have examples of all sorts of requests you would use to get different kinds of information.
https://github.com/justintv/Twitch-API/tree/master/v3_resources
One example of their requests is this:
curl -H 'Accept: application/vnd.twitchtv.v3+json' \
-X GET https://api.twitch.tv/kraken/chat/kraken_test_user
I have tried to do some research on requests, and I sort of understand some, but for the most part I could not find any resources that help make it click for me.
In the above example, what are the important parts of that request? curl? -H? Is that one big command, or is it two commands separated by the \ at the end of the first line?
Then, the biggest question, how to send requests like the one above using C#?
EDIT 1:
I also know that I will be getting responses in JSON. Is there anything built in that assists with receiving/parsing JSON?
And also using PUT to change some JSON? (some things in the API allow PUT).
For the first bit of the question, you asked what are the important parts
It has an accept header of application/vnd.twitchtv.v3+json
It is a GET request
The api url: https://api.twitch.tv/kraken/chat/kraken_test_user
This request in c# could look like the following (could because there is more than one way to do it)
private async Task<object> GetRequest(string url)
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.twitchtv.v3+json"));
var response = await httpClient.GetAsync(url);
var contents = await response.Content.ReadAsStringAsync();
return contents;
}
Note that the files in the link you posted are to Mark Down files that Google describes as:
MD, or markdown document is a text file created using one of several possible dialects of the Markdown language. MD files use plain text formatting but includes inline text symbols that define how to format the text, and is designed for authoring plain text documentation that can be easily converted to HTML.
curl -H 'Accept: application/vnd.twitchtv.v3+json' \
-X GET https://api.twitch.tv/kraken/chat/kraken_test_user
http://curl.haxx.se/docs/manpage.html explains what the curl command is that then has 2 switches, H and X. where quoting the link:
-H, --header
(HTTP) Extra header to include in the request when sending HTTP to a
server. You may specify any number of extra headers. Note that if you
should add a custom header that has the same name as one of the
internal ones curl would use, your externally set header will be used
instead of the internal one. This allows you to make even trickier
stuff than curl would normally do. You should not replace internally
set headers without knowing perfectly well what you're doing. Remove
an internal header by giving a replacement without content on the
right side of the colon, as in: -H "Host:". If you send the custom
header with no-value then its header must be terminated with a
semicolon, such as -H "X-Custom-Header;" to send "X-Custom-Header:".
curl will make sure that each header you add/replace is sent with the
proper end-of-line marker, you should thus not add that as a part of
the header content: do not add newlines or carriage returns, they will
only mess things up for you.
See also the -A, --user-agent and -e, --referer options.
Starting in 7.37.0, you need --proxy-header to send custom headers
intended for a proxy.
Example:
# curl -H "X-First-Name: Joe" http://192.168.0.1/
WARNING: headers set with this option will be set in all requests -
even after redirects are followed, like when told with -L, --location.
This can lead to the header being sent to other hosts than the
original host, so sensitive headers should be used with caution
combined with following redirects.
This option can be used multiple times to add/replace/remove multiple
headers.
The "\" makes the next line be added to the first line.
-X, --request
(HTTP) Specifies a custom request method to use when communicating
with the HTTP server. The specified request method will be used
instead of the method otherwise used (which defaults to GET). Read the
HTTP 1.1 specification for details and explanations. Common additional
HTTP requests include PUT and DELETE, but related technologies like
WebDAV offers PROPFIND, COPY, MOVE and more.
Normally you don't need this option. All sorts of GET, HEAD, POST and
PUT requests are rather invoked by using dedicated command line
options.
This option only changes the actual word used in the HTTP request, it
does not alter the way curl behaves. So for example if you want to
make a proper HEAD request, using -X HEAD will not suffice. You need
to use the -I, --head option.
The method string you set with -X will be used for all requests, which
if you for example use -L, --location may cause unintended
side-effects when curl doesn't change request method according to the
HTTP 30x response codes - and similar.
(FTP) Specifies a custom FTP command to use instead of LIST when doing
file lists with FTP.
(POP3) Specifies a custom POP3 command to use instead of LIST or RETR.
(Added in 7.26.0)
(IMAP) Specifies a custom IMAP command to use instead of LIST. (Added
in 7.30.0)
(SMTP) Specifies a custom SMTP command to use instead of HELP or VRFY.
(Added in 7.34.0)
If this option is used several times, the last one will be used.
In C#, there is a WebRequest class that https://msdn.microsoft.com/en-CA/library/456dfw4f(v=vs.110).aspx has a good example of how to use to get data from a given URL.
As for handling JSON, please look into http://www.newtonsoft.com/json which is a rather common library used for parsing JSON responses. PUT would be the HTTP verb like GET or POST used to tell the server how to process a request. I'd suggest in the future be careful about posting a rather broad set of questions here as I could see this being something that a class could spend an hour covering somewhere that I doubt your intention is getting someone else to do your homework, right?
Hi does anyone know how to use the streaming API for C#? Therefore, whenever there is a new tweet in my account, it will be reflected in my program.
So far the only reliable wrapper I've found for this in .Net land is TweetInvi. Try to ignore that the web site looks like it was designed by a hyperactive 10-year old (thanks MS 'metro' team), the actual library is very well designed and rock solid.
Assuming of course you have the relevant access tokens (if not see http://dev.twitter.com), an example of how easy it is to have up and running:
TwitterCredentials.SetCredentials(userToken,userTokenPrivate,apiKey,apiKeyPrivate);
_userStream = Stream.CreateUserStream();
_userStream.TweetCreatedByFriend += (sender,args) => Console.WriteLine(args.Tweet.Text);
_userStream.Start();
This will write the body of tweets to your console output, and it updates even faster than leaving the actual Twitter web site open. There are other events exposed for when a tweet is favourited, retweeted, when you have a new follower etc.
I can vouch for this library as being reliable - I am using it for my CovertTweeter project and have had absolutely no issues with it. In fact accessing the streaming API through TweetInvi has been even easier than the many brick walls I was left hitting when using REST wrappers like Linq2Twitter and TweetSharp.
Have a look at this post:
Streaming with New .NET HttpClient and HttpCompletionOption.ResponseHeadersRead
You don't have the complete implementation there but you will get the idea.
Here is a sample which "Reads data from the Twitter Streaming API and adds it to MSMQ. A second process (included) reads from the queue, parses the json message, and updates a data store."
https://github.com/swhitley/TwitterStreamClient
You can change the above problem to generate an event when it updates the data store. In your program you can subscribe this event to do whatever you want.
If you are looking for OAuth based sample then please use "AuthPack" which Provides .NET oAuth for Twitter, Facebook, LinkedIn, and Google:
https://github.com/swhitley/AuthPack/tree/master/AuthPack
I have found a good sample code that uses streaming API, here Twitterizer.
As of August 15, Amazon made it compulsory to sign all requests made to their Product Advertising API. I thought I had got everything working just fine but when the 15th finally came around, my web application stopped working and pretty much ever since I have been trying to find out how to sign the SOAP requests.
Amazon has an outdated sample code for signing requests that doesn't appear to work here
Basically, I need to know how to add a signature to the my requests using the most current C# SOAP API and .NET 3.5.
I hope I have given enough details, if I haven't please feel free to ask me to elaborate.
Thank You
The_Lorax
UPDATE:
I am using MVC and need to know how to add the Signature to the the ItemLookup or AWSECommerceService object. Is there an attribute that contains the signature value? How does it get attached to the request?
On this page, they say that I must include the Signature and TimeStamp parameters but the intellisense does now show any such attributes.
Check out http://flyingpies.wordpress.com/2009/08/01/17/. It has a walkthrough and a sample visual studio solution using C#, SOAP, WCF on .NET 3.5.
This library automatic sign the requests (Install-Package Nager.AmazonProductAdvertising)
https://www.nuget.org/packages/Nager.AmazonProductAdvertising/
Example:
var authentication = new AmazonAuthentication("accesskey", "secretkey");
var client = new AmazonProductAdvertisingClient(authentication, AmazonEndpoint.US);
var result = await client.SearchItemsAsync("canon eos");