I'm trying to set up offline access for a Google Analytics API application using OAuth2 and I'm using ASP.NET to POST my authorization code in exchange for a refresh token but I can't get a response from the server to give me the refresh token.
I've used the example code from the MSDN documentation for a post request so I can only assume this is correct, however I receive the error message "The remote server returned an error: (400) Bad Request at System.Net.HttpWebRequest.GetResponse()":
using System;
using System.IO;
using System.Net;
using System.Text;
WebRequest request = WebRequest.Create ("https://accounts.google.com/o/oauth2/token?code=xxxmyauthorizationcodexxx&client_id=xxxxxxxxx.apps.googleusercontent.com&client_secret=xxxxxxxxxxxx&redirect_uri=https://mysite.com/oauth2callback&grant_type=authorization_code");
request.Method = "POST";
string postData = "code=xxxmyauthorizationcodexxx&client_id=xxxxxxxxx.apps.googleusercontent.com&client_secret=xxxxxxxxxxxx&redirect_uri=https://mysite.com/oauth2callback&grant_type=authorization_code";
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream ();
dataStream.Write (byteArray, 0, byteArray.Length);
dataStream.Close ();
WebResponse response = request.GetResponse ();
dataStream = response.GetResponseStream ();
StreamReader reader = new StreamReader (dataStream);
string responseFromServer = reader.ReadToEnd ();
reader.Close ();
dataStream.Close ();
response.Close ();
I have successfully used the GET method to retrieve the authorization code to begin with and I'm following this documentation: https://developers.google.com/accounts/docs/OAuth2WebServer#handlingtheresponse
I'm also using a https website to make the requests and I'm manually refreshing my authorization code as it expires. Does anyone have a solution for this problem?
EDIT: For anyone experiencing the same issue, firstly look at aeijdenberg's response below, but my solution was that the authorization code I used for the code parameters expires pretty instantaneously - I kept refreshing my page without request a new one. To get the data just display the content from the responseFromServer variable.
It looks like you are passing the parameters twice. Once in the query string:
WebRequest request = WebRequest.Create ("https://accounts.google.com/o/oauth2/token?code=xxxx...
And then again as POST data. I would suggest removing the query string, e.g. POST directly to "https://accounts.google.com/o/oauth2/token".
Also suggesting ensuring that all parameters are URL encoded if you're not already doing so:
http://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode.aspx
Related
I'm developing an intranet (WRITTEN IN C#) which is going to gather all software applications used in my company. Some of these applications are not internal (so I can't actually see nor manage the source code).
I have to "bypass" a login page of an external application (WRITTEN IN PHP), sending username and password from asp.net (my intranet).
I don't really know HOW to manage this, IF possibile..
I only know that it's expecting a $_POST["l_username"] and a $_POST["l_passowrd"].
I've been looking for a solution for hours now..and still..nothing seems to be working. I read maaany post but there are not useful in my case.
EDIT 1:
public void sendInfo(string url, string data)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
string Data = data;
byte[] postBytes = Encoding.ASCII.GetBytes(Data);
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = postBytes.Length;
Stream requestStream = req.GetRequestStream();
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream resStream = response.GetResponseStream();
var sr = new StreamReader(response.GetResponseStream());
string responseText = sr.ReadToEnd();
req.AllowAutoRedirect = true;
}
This is the code I've been trying to use when I click on the link (I'm using a LinkButton)..but it is not redirecting me to the page. It's supposed to redirect, logging in using the parameters I give in Data and show me the main page of the external application..any suggestions would be greatly appreciated!
EDIT 2:
I found a code which seems to be working, finally!
You can find it ->HERE<-.
I tried it using a simple Web Form (with a LinkButton) and a class, it works perfectly.
My problem now is that my intranet uses a MasterPage, and when I'm calling the method from a Content Page..nothing happens.
What can I do to have this code working on a Content Page?
You can specify the POST method for WebRequest (more details in this SO answer: curl Request with ASP.NET )
You should build a Request with the ASP.net WebRequest. You know which data to send, so that shouldn't be a problem!
At the end I used this ->code here<- and it worked just fine.
Couldn't understand why it won't work using master and content pages, but I managed without them.
i am using mvaayoo api for sending the messages from my website.
i have read the documentation and do the same even then i am not able to send the messages.
i am using this sample code
string strUrl = "http://api.mVaayoo.com/mvaayooapi/MessageCompose?user=
Username:Password&senderID=mVaayoo&receipientno=919849558211&msgtxt=This is a test from mVaayoo API&state=4";
WebRequest request = HttpWebRequest.Create(strUrl);
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse();
Stream s = (Stream)response.GetResponseStream();
StreamReader readStream = new StreamReader( s );
string dataString = readStream.ReadToEnd();
response.Close();
s.Close();
readStream.Close();
Help me please
Thanks,
Rajbir
I think the problem is because of the space in URL.
Use this for URL ;
string strUrl = "http://api.mVaayoo.com/mvaayooapi/MessageCompose?user=
Username:Password&senderID=mVaayoo&receipientno=919849558211&msgtxt=This%20is%20a%20test%20from%20mVaayoo%20API&state=4";
I replaced all space with %20 in the URL. I suffered with the same problem in java.. so it should work..
I have a ASP page where there is a link which is passed to another domain written in JSP.
I need to pass a parameter from ASP page to JSP page but it should be hidden from being caught by user.
On other side in JSP page I will retrieve the parameter and use it accordingly. How can I achieve this?
You could do a WebRequest in the .NET page to the JSP page, set whatever you need on the server side JSP, then redirect to a JSP URL with the server side state already in place.
// Create a request for the URL.
WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
// Display the status.
Console.WriteLine (response.StatusDescription);
// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Cleanup the streams and the response.
reader.Close ();
dataStream.Close ();
response.Close ();
//redirect after
I'm using HttpWebRequest to send request without using a browser but I always get this reply from this old school cgi forum: "duplicate thread". I'm pretty sure the thread is not a duplicate as it is the first time I send it. I'm guessing the forum software is detecting something unusual about my thread as is is machine generated. What can I do?
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://pop.6park.com/cgi-bin/know1/mainboard.pl");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
string postData = "name=ZhuangNan&usrpwd=aaa&subject=whatisthis&body=abcabcabcabc";
UTF8Encoding encoding = new UTF8Encoding();
byte[] byte1 = encoding.GetBytes(postData);
myRequest.ContentLength = byte1.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(byte1, 0, byte1.Length);
If the target script tries to check the UserAgent HTTP header to determine if the request is being made by a browser, you can try populating the HttpWebRequest.UserAgent property with a valid value. This has worked for me on at least one occasion.
There are a number of other HTTP headers that you could set to make the target script think it is being hit by a browser rather than a program.
Check out any of the HTTP header properties that can be set. See HttpWebRequest Properties for more information.
myRequest.ContentType = "application/x-www-form-urlencoded";
string postData = "name=ZhuangNan&usrpwd=aaa&subject=whatisthis&body=abcabcabcabc";
Not a MIME/baseenc data?
I'm trying to ping Google when my web site's sitemap is updated but I need to know which status code does Google or any other service returns. My code is below:
HttpWebRequest rqst = (HttpWebRequest)WebRequest.Create("http://search.yahooapis.com/ping?sitemap=http%3a%2f%2fhasangursoy.com.tr%2fsitemap.xml");
rqst.Method = "POST";
rqst.ContentType = "text/xml";
rqst.ContentLength = 0;
rqst.Timeout = 3000;
rqst.GetResponse();
You need to use the response - assign it to a HttpWebResponse variable:
HttpWebResponse resp = (HttpWebResponse)rqst.GetResponse();
HttpStatusCode respStatusCode = resp.StatusCode;
The HttpStatusCode enumeration will tell you what status code was returned.
Try HttpWebResponse.StatusCode out