I am trying to upload data to a REST-Service via POST-Method, but for some reasons the server tells me:
System.Net.WebException: The remote server returned an error: NotFound.
I am trying to upload data with this code:
WebClient addserving = new WebClient();
addserving.Credentials = new NetworkCredential(username.Text, passwort.Password);
addserving.Encoding = System.Text.Encoding.GetEncoding("ISO-8859-1");
addserving.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
addserving.UploadStringAsync(new Uri("http://jokr.info/api/v8/diary/add_item.xml&apikey=123456&item_id=1240&serving_id=1566"), "POST");
addserving.UploadStringCompleted += new UploadStringCompletedEventHandler(serving_UploadStringCompleted);
The doc of the API tells me to post like this:
Rate Limit: Yes
HTTP Methods: POST
Authentication: Basic authentication (Username or E-Mail and Password)
Formats: xml
Parameters: format, apikey [GET], activity_id [POST], activity_duration [POST], activity_kj [POST], timestamp [POST] (optional)
Does anyone see what's wrong?
Shouldn't you have a ? before stating query parameters instead of &
http://jokr.info/api/v8/diary/add_item.xml?apikey=123456&item_id=1240&serving_id=1566
You're missing a question mark to mark the beginning of hte parameter collection.
Change
http://jokr.info/api/v8/diary/add_item.xml&apikey=123456&item_id
to
http://jokr.info/api/v8/diary/add_item.xml?apikey=123456&item_id
Related
When I upload an image, the sharepoint will give a link corresponding to that image.
I am working on a C # project that analyzes images, and want to use SHarepoint's image link.
But when executing the function to load the image from WebClient's url (), I was blocked by an error.
Error name: "the remote server returned an error (401) unauthorized"
this is image show link in my Sharepoint (the link that I am scanning): https://ibb.co/g9jYHKC
And this is code webclient() I used:
var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData("http://pmssd78/Animal/birddd.jpg"); //link copy from sharepoint like image show
Looking forward to hearing from everybody soon, thanks
HTTP 401 means unauthorized, which means you aren't authenticated with the resource that you are making the request to. You need to authenticate with the server for it to accept your request.
With the WebClient class, you can do this via the Credentials class member:
//Create a Credential Cache - you'll want this to be defined somewhere appropriate,
//maybe at a global level so other parts of your application can access it
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri("http://yourSharepointUrl.com"), "Basic", new NetworkCredential("yourUserName", "yourSecuredPassword"));
//Create WebClient, set credentials, perform download
WebClient webClient = new WebClient();
webClient.Credentials = credentialCache.GetCredential(new Uri("http://yourSharepoint.com"),
"Basic");
byte[] imageBytes = webClient.DownloadData("http://pmssd78/Animal/birddd.jpg");
Likely, the Uri used in the credential cache is going to be something along the lines of your URI being used in the .DownloadData signature.
I used JSON to pass my variables as per the request of my API provider... I am attaching the code below:
string url = "http://api-v2.happay.in/auth/v1/adduser";
string token = "fcd853a15a6e97b8834255dde74cd99527";
string dataToUpload = "{"requestedId":"12389","userId":"134474","firstName":"Maredu Laxmi","lastName":"","emailId":"arar#gmail.com","mobileNo":"096518644","dob":"1978-02-26","gender":"Female","title":"Ms","password":"","metaFields":{"EmpId":"12389","Grade":"D6","Location":"Hyderabad"},"supervisors":[{"supervisorId":"1024","roleName":"RP"},{"supervisorId":"1025","roleName":"RP"}]}";
var cli = new WebClient();
cli.Headers[HttpRequestHeader.ContentType] = "application/json";
cli.Headers[HttpRequestHeader.Authorization] = "Bearer " + token;
string response = cli.UploadString(url, "POST", dataToUpload);
What am I doing wrong?
405 Method Not Allowed means that the endpoint you're trying to call (http://api-v2.happay.in/auth/v1/adduser) doesn't support the method you're trying to use (POST).
From RFC 7231, Section 6.5.5:
The 405 (Method Not Allowed) status code indicates that the method received in the request-line is known by the origin server but not supported by the target resource.
Are you sure the endpoint supports the POST method?
The RFC also states that
The origin server MUST generate an Allow header field in a 405 response containing a list of the target resource's currently supported methods.
You could check the response and see if they've followed the specification and included an Allow header.
I am a bit confused that when I get the error:
Problem during authentication process, check headers!
Unable to authenticate user, incorrect token
There is definitely something wrong with request header.
Can any one tell me the correct way of sending a request to GetResponse API?
I am using this way:
var request = new RestRequest("/campaigns", Method.GET);
request.AddHeader("X-Auth-Token", "api-key " + auth.myAuthorizationKey);
The header looks like this in request while debugging:
{X-Auth-Token=api-key d042eeae34ce076913681cc5c872741e2c5f88d2}
Use APIKEY instead AuthorizationKey
I need to generate an http request to the vimeo api as per step 2 in this page, which is given below.
PUT https://i.cloud.vimeo.com/video/518016424
.... binary data of your file in the body ....
I alredy have an access token for this. Suppose the access token is "qw21we34". How do I generate an http request, with the token in the header and binary data in the body.
I tried using WebClient() class as suggested here, but I cannot find a method to pass the OAuth access token with this type of request. Please note that there are no official libraries for Vimeo api that have this facility. Can anyone help?
For this you can use the WebClient() class. For the authentication, we need the access token from the previous request as well. I got it from my VimeoClient object called vc. That is up to you to figure out.
WebClient wb = new WebClient();
wb.Headers.Add("Authorization","Bearer" +vc.AccessToken);
var file = wb.DownloadData(new Uri(myimageurl));
var asByteArrayContent = wb.UploadData(new Uri(thumbnail_uri), "PUT", file);
var asStringContent = Encoding.UTF8.GetString(asByteArrayContent);
After sending this request, you should get a json response stating the success for asStringContent.
I am trying to call Google's OAuth2 authentication service as per these instructions: https://developers.google.com/accounts/docs/OAuth2ForDevices
I put all of the required parameters into the query string and sent the request. This worked for the "Obtaining a user code" section but not for the "Obtaining Access and Refresh Tokens" section.
After much playing around and getting 400 Bad Request errors, I found that, instead of putting the data in the query string, you can create a request with a FormUrlEncodedContent and send the data through as content with application\x-www-form-urlencoded Content-Type.
Here is the code before:
var requestMessage = new HttpRequestMessage();
requestMessage.Method = "POST";
requestMessage.RequestUri = new Uri(fullUrl);
Where fullUrl is something like:
https://accounts.google.com/o/oauth2/device/code?client_id=812741506391-h38jh0j4fv0ce1krdkiq0hfvt6n5amrf.apps.googleusercontent.com&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email%20https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile
And the new code is:
var requestMessage = new HttpRequestMessage();
requestMessage.Method = "POST";
requestMessage.RequestUri = new Uri(url);
requestMessage.Content = new FormUrlEncodedContent(CreateDictionary(queryStringNames, queryStringValues));
Where url is:
https://accounts.google.com/o/oauth2/device/code
and queryStringNames and queryStringValues are string arrays of the names and values of the required parameters.
What is the difference between these two methods? Is it safe to assume that all POST calls can use the URL Encoded Content requests instead of putting the data in the query string?
In general, POST requests do not need query string but it is still subjected to Server's logic implementation. In case of OAuth which is quite known standard and they do follow good practice, it is safe to use form encoded data unless mentioned explicitly in API to send Parameter as query string.
Query String & Post data are two different set of parameters. If server is expecting Query string then you must send query string only. It all depends on how server side logic is implemented. You can not use them alternatively. Most API documentation specify clearly what are they expecting.