oauth get request token for google shows "The page you requested is invalid. " - servlets

I am trying to send a request to oauth google to get a request token.i passed all the parameters required.here is the code.please help.
The out put of the below code is a token provided by google to access its users private data.but i am getting the result that the page i requested is invalid.where did i go wrong?
should i pass the scope also in the url?
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
try {
String oauthConsumerKey = "my key";
String oauthSignatureMethod = "HMAC-SHA1";
String oauthSignature = "my signature";
// Send the request
URL url = new URL("https://www.google.com/accounts/OAuthGetRequestToken"+oauthConsumerKey+
oauthSignatureMethod+oauthSignature);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
//write parameters
writer.flush();
// Get the response
StringBuffer answer = new StringBuffer();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println("\n" +line);
}
writer.close();
reader.close();
//Output the response
} catch (MalformedURLException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

As specified in the documentation of the OAuthGetRequestToken call, you are missing some required parameters:
oauth_consumer_key (required)
oauth_nonce (required)
oauth_signature_method (required)
oauth_signature (required)
oauth_timestamp (required)
scope (required)
oauth_callback (required)
Your query URL also has to be in a format like:
https://www.google.com/accounts/OAuthGetRequestToken?param1=value1&param2=value2&...
Please note that the Google OAuth 1.0 API is already deprecated and should not be used for new projects:
Important: OAuth 1.0 has been officially deprecated as of April 20, 2012. It will continue to work as per our deprecation policy, but we encourage you to migrate to OAuth 2.0 as soon as possible.
Further the OAuth 2 flows are way more simple, check out the documentation at Google.

Related

Send push notifications from server with FCM

Recently I asked a question on sending push notifications using GCM: Send push notifications to Android. Now that there is FCM, I am wondering how different it would be from the server side development. Coding wise, are they the same? Where can I find example FCM codes showing sending push notifications from server to Android device?
Do I need to download any JAR library for sending notifications to FCM using Java codes? The example codes in Send push notifications to Android shows sending push notifications using GCM and a server side GCM JAR file is required.
However, another example in https://www.quora.com/How-do-I-make-a-post-request-to-a-GCM-server-in-Java-to-push-a-notification-to-the-client-app shows sending push notifications using GCM and no server side GCM JAR file is required since it is just sending via an HTTP connection. Can the same codes be used for FCM? The URL used is "https://android.googleapis.com/gcm/send". What would be the equivalent URL for FCM?
How different is server-side coding?
Since there is not much difference, you can just check out most of the example server-side codes for GCM as well. Main difference with regards to GCM and FCM is that when using FCM, you can use the new features with it (as mentioned in this answer). FCM also has a Console where you can send the Message/Notification from, without having your own app server.
NOTE: Creating your own app server is up to you. Just stating that you can send a message/notification via the console.
The URL used is "https://android.googleapis.com/gcm/send". What would be the equivalent URL for FCM?
The equivalent URL for FCM is https://fcm.googleapis.com/fcm/send. You can check out the this doc for more details.
Cheers! :D
Use below code to send push notification from FCM server :
public class PushNotifictionHelper {
public final static String AUTH_KEY_FCM = "Your api key";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public static String sendPushNotification(String deviceToken)
throws IOException {
String result = "";
URL url = new URL(API_URL_FCM);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", "key=" + AUTH_KEY_FCM);
conn.setRequestProperty("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("to", deviceToken.trim());
JSONObject info = new JSONObject();
info.put("title", "notification title"); // Notification title
info.put("body", "message body"); // Notification
// body
json.put("notification", info);
try {
OutputStreamWriter wr = new OutputStreamWriter(
conn.getOutputStream());
wr.write(json.toString());
wr.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
result = CommonConstants.SUCCESS;
} catch (Exception e) {
e.printStackTrace();
result = CommonConstants.FAILURE;
}
System.out.println("GCM Notification is sent successfully");
return result;
}
This is coming straight from Google
You won’t need to make any server-side protocol changes for the upgrade. The service protocol has not changed. However, note that all new server enhancements will be documented in FCM server documentation.
And from receiving messages it seams there is only some places where its only slightly different. Mainly deleting somethings.
And the FCM server documentation can be found here
https://firebase.google.com/docs/cloud-messaging/server
FULL SOLUTION FOR TOPIC, SINGLE DEVICE AND MULTIPLE DEVICES
Create a class FireMessage. This is an example for data messages. You can change data to notification.
public class FireMessage {
private final String SERVER_KEY = "YOUR SERVER KEY";
private final String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
private JSONObject root;
public FireMessage(String title, String message) throws JSONException {
root = new JSONObject();
JSONObject data = new JSONObject();
data.put("title", title);
data.put("message", message);
root.put("data", data);
}
public String sendToTopic(String topic) throws Exception { //SEND TO TOPIC
System.out.println("Send to Topic");
root.put("condition", "'"+topic+"' in topics");
return sendPushNotification(true);
}
public String sendToGroup(JSONArray mobileTokens) throws Exception { // SEND TO GROUP OF PHONES - ARRAY OF TOKENS
root.put("registration_ids", mobileTokens);
return sendPushNotification(false);
}
public String sendToToken(String token) throws Exception {//SEND MESSAGE TO SINGLE MOBILE - TO TOKEN
root.put("to", token);
return sendPushNotification(false);
}
private String sendPushNotification(boolean toTopic) throws Exception {
URL url = new URL(API_URL_FCM);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", "key=" + SERVER_KEY);
System.out.println(root.toString());
try {
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(root.toString());
wr.flush();
BufferedReader br = new BufferedReader(new InputStreamReader( (conn.getInputStream())));
String output;
StringBuilder builder = new StringBuilder();
while ((output = br.readLine()) != null) {
builder.append(output);
}
System.out.println(builder);
String result = builder.toString();
JSONObject obj = new JSONObject(result);
if(toTopic){
if(obj.has("message_id")){
return "SUCCESS";
}
} else {
int success = Integer.parseInt(obj.getString("success"));
if (success > 0) {
return "SUCCESS";
}
}
return builder.toString();
} catch (Exception e) {
e.printStackTrace();
return e.getMessage();
}
}
}
And call anywhere like this. Both server and android we can use this.
FireMessage f = new FireMessage("MY TITLE", "TEST MESSAGE");
//TO SINGLE DEVICE
/* String fireBaseToken="c2N_8u1leLY:APA91bFBNFYDARLWC74QmCwziX-YQ68dKLNRyVjE6_sg3zs-dPQRdl1QU9X6p8SkYNN4Zl7y-yxBX5uU0KEKJlam7t7MiKkPErH39iyiHcgBvazffnm6BsKjRCsKf70DE5tS9rIp_HCk";
f.sendToToken(fireBaseToken); */
// TO MULTIPLE DEVICE
/* JSONArray tokens = new JSONArray();
tokens.put("c2N_8u1leLY:APA91bFBNFYDARLWC74QmCwziX-YQ68dKLNRyVjE6_sg3zs-dPQRdl1QU9X6p8SkYNN4Zl7y-yxBX5uU0KEKJlam7t7MiKkPErH39iyiHcgBvazffnm6BsKjRCsKf70DE5tS9rIp_HCk");
tokens.put("c2R_8u1leLY:APA91bFBNFYDARLWC74QmCwziX-YQ68dKLNRyVjE6_sg3zs-dPQRdl1QU9X6p8SkYNN4Zl7y-yxBX5uU0KEKJlam7t7MiKkPErH39iyiHcgBvazffnm6BsKjRCsKf70DE5tS9rIp_HCk");
f.sendToGroup(tokens); */
//TO TOPIC
String topic="yourTopicName";
f.sendToTopic(topic);
I have created a lib for FCM notification Server. Just use it like GCM lib.
For FCM Server use this code :
GCM Server URL-"android.googleapis.com/gcm/send"
FCM Server URL - "fcm.googleapis.com/fcm/send"
Append https with URL
Sender objSender = new Sender(gAPIKey);
or
Sender objSender = new Sender(gAPIKey,"SERVER_URL");
by DEFAULT FCM SERVER URL IS ASSIGNED
Message objMessage = new Message.Builder().collapseKey("From FCMServer").timeToLive(3).delayWhileIdle(false)
.notification(notification)
.addData("ShortMessage", "Sh").addData("LongMessage", "Long ")
.build();
objMulticastResult = objSender.send(objMessage,clientId, 4);
Dependency need for this lib is same like GCM lib required (jsonsimple.jar).
Download lib from FCM_Server.jar
public class SendPushNotification extends AsyncTask<Void, Void, Void> {
private final String FIREBASE_URL = "https://fcm.googleapis.com/fcm/send";
private final String SERVER_KEY = "REPLACE_YOUR_SERVER_KEY";
private Context context;
private String token;
public SendPushNotification(Context context, String token) {
this.context = context;
this.token = token;
}
#Override
protected Void doInBackground(Void... voids) {
/*{
"to": "DEVICE_TOKEN",
"data": {
"type": "type",
"title": "Android",
"message": "Push Notification",
"data": {
"key": "Extra data"
}
}
}*/
try {
URL url = new URL(FIREBASE_URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Authorization", "key=" + SERVER_KEY);
JSONObject root = new JSONObject();
root.put("to", token);
JSONObject data = new JSONObject();
data.put("type", "type");
data.put("title", "Android");
data.put("message", "Push Notification");
JSONObject innerData = new JSONObject();
innerData.put("key", "Extra data");
data.put("data", innerData);
root.put("data", data);
Log.e("PushNotification", "Data Format: " + root.toString());
try {
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(root.toString());
writer.flush();
writer.close();
int responseCode = connection.getResponseCode();
Log.e("PushNotification", "Request Code: " + responseCode);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader((connection.getInputStream())));
String output;
StringBuilder builder = new StringBuilder();
while ((output = bufferedReader.readLine()) != null) {
builder.append(output);
}
bufferedReader.close();
String result = builder.toString();
Log.e("PushNotification", "Result JSON: " + result);
} catch (Exception e) {
e.printStackTrace();
Log.e("PushNotification", "Error: " + e.getMessage());
}
} catch (Exception e) {
e.printStackTrace();
Log.e("PushNotification", "Error: " + e.getMessage());
}
return null;
}
}
Use
SendPushNotification sendPushNotification = new SendPushNotification(context, "token");
sendPushNotification.execute();

Apache Abdera Multipart Request throwing nullpointer Exception(IBM connection API)

I am using Apache abdera to post multipart request to IBM connection 4.0 API. I am getting nullpointer exception from Abdera API. Please let me know what's the root cause.
private void createEntryWithAttachment(){
try {
String activityId = "urn:lsid:ibm.com:oa:662d0dc7-0308-48ee-8291-d730c733d2d1";
String activityIdLocal = activityId.substring(activityId.lastIndexOf(":")+1, activityId.length());
String createEntryLocal = createEntry+activityIdLocal;
Abdera abdera = new Abdera();
AbderaClient client = new AbderaClient(abdera);
AbderaClient.registerTrustManager();
System.out.println("pd --->"+pd);
client.addCookie("poktam2cl.iespc.ibm.com", "PD-S-SESSION-ID", pd, "/", null, true);
RequestOptions requestOptions = client.getDefaultRequestOptions();
requestOptions.setUseChunked(true);
requestOptions.setHeader("Connection", "close");
requestOptions.setHeader("Content-Type", "multipart/related;type=\"application/atom+xml\"");
requestOptions.setContentType("multipart/related;type=\"application/atom+xml\"");
requestOptions.setSlug("Sample.txt");
Credentials credentials = new UsernamePasswordCredentials(username, password);
client.addCredentials(createEntryLocal, AuthScope.ANY_REALM,AuthScope.ANY_SCHEME, credentials);
Entry entry = abdera.getFactory().newEntry();
entry.setTitle("create entry with attachment title ");
entry.setContent("create entry with attachment content");
javax.xml.namespace.QName field = new QName("http://www.ibm.com/xmlns/prod/sn", "field", "snx");
org.apache.abdera.model.Element fieldElement = entry.addExtension(field);
fieldElement.setAttributeValue("type", "file");
fieldElement.setAttributeValue("name", "sampletextfile1");
fieldElement.setAttributeValue("position", "3000");
FileInputStream fis = new FileInputStream(filepath);
requestOptions.setHeader("Content-Length", "35");
entry.addCategory("http://www.ibm.com/xmlns/prod/sn/type","entry", "Entry");
ClientResponse response = client.post(createEntryLocal, entry, fis, "multipart/related;type=\"application/atom+xml\"", requestOptions );
System.out.println("Entry Created with attachment's resp: " + response.getStatus());
if(response.getStatus() == 201){
System.out.println("Entry Created with attachment successfully .....");
printIBMConnectionErrorMessage(response);
}else{
System.out.println("Entry with attachment creation failed");
printIBMConnectionErrorMessage(response);
//System.exit(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Output
java.lang.NullPointerException
at org.apache.abdera.protocol.client.util.MultipartRelatedRequestEntity.writeInput(MultipartRelatedRequestEntity.java:74)
at org.apache.abdera.protocol.client.util.MultipartRelatedRequestEntity.writeRequest(MultipartRelatedRequestEntity.java:59)
at org.apache.commons.httpclient.methods.EntityEnclosingMethod.writeRequestBody(EntityEnclosingMethod.java:499)
at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2114)
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323)
at org.apache.abdera.protocol.client.AbderaClient.execute(AbderaClient.java:688)
at org.apache.abdera.protocol.client.AbderaClient.post(AbderaClient.java:306)
at JavaAgentEntryWithAttachment.createEntryWithAttachment(JavaAgentEntryWithAttachment.java:157)
at JavaAgentEntryWithAttachment.main(JavaAgentEntryWithAttachment.java:66)
This exception is coming from abdera API, class called MultipartRelatedRequestEntity.java, Line no 74. I have placed line no 74 source code below. So its clear that contentSrc is null & Abdera API not allowing me to set this value. Please let me know what I am missing here.
String contentId = entry.getContentSrc().toString();
I did in two steps:
Send the file
Call to update the data
Each with the good mime type. You can not send the file with XML mime type. And put the length of the file.
It is possible to avoid the nullpointer and do it in one request. I had the same issue and created another issue and managed to find a solution. You can find it here.
It comes down to the following code example where you create a HttpClient Part which can contain a StringPart and a FilePart
final Entry entry = // ... Create your Entry
final RequestOptions options = this.client.getDefaultRequestOptions();
options.setHeader("Content-Type", "multipart/related;type=\"application/atom+xml\"");
StringPart entryPart = new StringPart("entry", entry.toString());
entryPart.setContentType("application/atom+xml");
FilePart filePart = new FilePart("file", new File(resource.getFile()));
RequestEntity request = new MultipartRequestEntity(new Part[] { entryPart, filePart}, this.client.getHttpClientParams());
ClientResponse response = client.post(this.url + this.activityId, request, options);
Hope this will help people in the future if they are using Abdera.

INVALID paypal ipn response however [duplicate]

This question already exists:
PayPal IPN Sandbox response always INVALID
Closed 9 years ago.
I am using sandbox mode. I have a buy now button in sandbox mode linked to my sandbox business account which has ipn enabled with the url to my site. The ipn implementation is exactly the same as the sample code here: https://cms.paypal.com/cms_content/GB/en_GB/files/developer/IPN_ASP_NET_C.txt
I click the button and make a purchase using a sandbox personal account which is successful. it shows up as sent with code 200 in the business accounts ipn history but on the ipn page on my site the response is invalid.
Been at this for days now.. cant figure it out :(
Here's what I'm using for https://ASPSecurityKit.net
private void ProcessPayment(bool test)
{
try
{
string callbackResponse = null;
string content = null;
string callbackUrl = test ? "https://www.sandbox.paypal.com/cgi-bin/webscr"
: "https://www.paypal.com/cgi-bin/webscr";
var req = (HttpWebRequest) WebRequest.Create(callbackUrl);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
content = Encoding.ASCII.GetString(
Request.BinaryRead(HttpContext.Request.ContentLength)
);
content += "&cmd=_notify-validate";
req.ContentLength = content.Length;
using (var streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII))
{
streamOut.Write(content);
}
using (var streamIn = new StreamReader(req.GetResponse().GetResponseStream()))
{
callbackResponse = streamIn.ReadToEnd();
}
if (callbackResponse.Equals("VERIFIED", StringComparison.OrdinalIgnoreCase))
{
// Now validate whether gross_amount is ok, receiver_email is your business acount mail id and so on.
}
}
catch (Exception ex)
{
// Logger.Log(ex); // Uncomment this line if you have a logger
}
}
Note: I store all transactions in the database whether varified or invalid. That logic is ASPSecurityKit.net specific hence I have omitted that here.

Passing an image from a Wep Api service to another Web Api service

For security reasons, I am building two Web Api services. The first Web Api app will have access to an image generating service, and will act as a security proxy. The second Web Api app will call the first app from the internet and retrieve the image.
However, I can't seem to get to negotiate passage of the image correctly. My thought was to have the security proxy Web API to get the image, and then pass it as a byte array my other service which would allow a user to download the image. However, when my browser attempts to open the image, it is always corrupted.
Here is the security proxy getting the image, which I know is successful:
public byte[] Get(string invoice, string Customer)
{
object image;
try
{
image = _repo.GetImage(invoice, Customer);
}
catch (ApplicationException exc)
{
var resp = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent(string.Format("No Image with Invoice Number = {0}", invoice.ToString())),
ReasonPhrase = "Image Not Found"
};
throw new HttpResponseException(resp);
}
catch (Exception exc)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
}
return (byte[])image;
}
This returns an array with a length of 40133.
The calling Web API service looks like this:
public HttpResponseMessage Get(string invoice, string Customer)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));
byte[] img = client.GetByteArrayAsync("http://localhost:1363/api/Image/" + invoice + "/" + Customer).Result;
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new ByteArrayContent(img);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/tiff");
var disposition = new ContentDispositionHeaderValue("attachment");
disposition.FileName = "ImageDocument.tif";
response.Content.Headers.ContentDisposition = disposition;
return response;
}
However, the length of the img byte array is 53514.
When the browser tries to open the image, it tells me it is corrupt. If I open the TIFF in notepad, I get :
"SUkqAAgAAAASAP4ABAABAAAAAAAAAAABBAABAAAAsAYAAAEBBAABAAAAvgQAAAIBAwABAAAAAQAAAAMBAwABAAAABAAAAAYBAwABAAAAAAAAAAcBAwABAAAAAQAAABEBBAABAAAAAAMAABIBAwABAAAAAQAAABUBAwABAAAAAQAAABYBBAABAAAAvgQAABcBBAABAAAAxZkAABoBBQABAAAA+AIAABsBBQABAAAA8AIAACgBAwABAAAAAgAAADEBAgA4AAAAuAIAADIBAgAUAAAApAIAAOiAAwABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADIwMTI6MTA6MDMgMDc6Mjc6MTkAS29mYXggc3RhbmRhcmQgTXVsdGktUGFnZSBUSUZGIFN0b3JhZ2UgRmlsdGVyIHYzLjAzLjAwMADIAAAAAQAAAMgAAAABAAAALcMjAGC+cBQRwhOKcIuzqZDzrHoxF8k+VAOR2cAgjhC5lQEI+VYoiIiIiIiIiJXLAazgaZvMBqEcNI0BoJwaTMGsjgqGA1yOGaUA0Hg0igC5qZ6I1GSsNMuGqeBrI+bBoNYQrfNIiMREWdl4zVWRERkQzVECBpNcRyMCz6PhQgZwQGQLjpCIWwgxERGLLYAx//zLWLx4IeDnBnxSGFMRgIeZ4zcaR+KuPM4KeZ6MBTqKcj8YjAQ4IejDoQ4eE07WGnra3p9w07Xhw1s7NHu+0/v+/SQf6/9+cjwp0Z0Z8KeCm4p4IGQwhoz4cwCFBZN8u8s5duXeXTLva7pN6J56l45sf8u8u
SNIP*
Anyone know what I am doing wrong?
Thanks!
Chris
Solved
If anyone is interested in the calling code that leverages the solution identified, here it is:
public HttpResponseMessage Get(string invoice, string Customer)
{
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("image/tiff"));
byte[] img = client.GetByteArrayAsync("http://localhost:1363/api/Image/" + invoice + "/" + Customer).Result;
HttpResponseMessage response = new HttpResponseMessage();
response.Content = new ByteArrayContent(img);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/tiff");
var disposition = new ContentDispositionHeaderValue("attachment");
disposition.FileName = "ImageDocument.tif";
response.Content.Headers.ContentDisposition = disposition;
return response;
}
With your above current return type (byte[]) of action, formatters of web api are probably handling them and hence you are seeing unexpected response.
can you try sending the image as a ByteArrayContent instead?(you need to have HttpResponseMessage as a return type here)
Example:
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(..your byte array here...);
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpeg");
return response;

MailChimp oauth2 in ASP.NET keeps returning invalid_grant

I am developing a new app that offers integration with MailChimp. Basically, it enables users to easily export their customer contact info directly to a MailChimp account (that is, to a specific mailing-list inside MailChimp).
All that works, and are somewhat irrelevant to my question.
For not asking the user to enter MailChimp-credentials every time, I'm about to implement the oauth2 authorization workflow as described here: http://apidocs.mailchimp.com/oauth2/
It works just fine in step 1-3, but step 4 is killing me.
It's my first time working with oauth, but I seem to understand the basics.
Here is my problem:
When I do the POST call to the https://login.mailchimp.com/oauth2/token -URI, to get the final access-token, I keep getting the error in JSON result: "invalid_grant"
I have checked the request and response streams, that my url is compiled correctly.
Here is my code in the controller:
(GrantEcoAccess is just to grant access to another app - the rest should be self-explaining)
public class HomeController : ApplicationController
{
private readonly string authorize_uri = "https://login.mailchimp.com/oauth2/authorize";
private readonly string access_token_uri = "https://login.mailchimp.com/oauth2/token";
private readonly string mailchimp_clientid2 = "xxx";
private readonly string mailchimp_secret2 = "yyy";
...
public ActionResult GrantEcoAccess()
{
//if exist: use saved token
var user = (Mailchimp_users)Session["user"];
if (!string.IsNullOrWhiteSpace(user.EcoToken))
return RedirectToAction("GrantMailChimpAccess");
// if !
var url = "https://secure.e-conomic.com/secure/api1/requestaccess.aspx?role=superuser&appId=MailChimp&redirectUrl=http://localhost:18017/Home/IncomingToken";
Redirect(url).ExecuteResult(ControllerContext);
return null;
}
public ActionResult IncomingToken(string token)
{
var user = (Mailchimp_users)Session["user"];
user.EcoToken = token;
EcoSession.DataSession.Refresh(System.Data.Objects.RefreshMode.ClientWins, user);
EcoSession.DataSession.SaveChanges();
return RedirectToAction("GrantMailChimpAccess");
}
public ActionResult GrantMailChimpAccess()
{
//if exist: use saved token
var user = (Mailchimp_users)Session["user"];
if (!string.IsNullOrWhiteSpace(user.MailChimpToken))
return RedirectToAction("Index", "Subscribe");
//if !
var url = string.Format("{0}?response_type=code&client_id={1}&redirect_uri=", authorize_uri, mailchimp_clientid2, "http://127.0.0.1:18017/Home/IncomingMailChimpToken");
Redirect(url).ExecuteResult(ControllerContext);
return null;
}
public ActionResult IncomingMailChimpToken(string code)
{
var url = "https://login.mailchimp.com/oauth2/token?grant_type=authorization_code&client_id=XX&client_secret=XX&code=" + code + "&redirect_uri=http://127.0.0.1:18017/Home/AuthComplete";
//var url = string.Format("?grant_type=authorization_code&client_id={0}&client_secret={1}&code={2}&redirect_uri={3}", mailchimp_clientid, mailchimp_secret, code, Url.Action("AuthComplete"));
Response.Clear();
StringBuilder sb = new StringBuilder();
sb.Append("<html>");
sb.AppendFormat(#"<body onload='document.forms[""form""].submit()'>");
sb.AppendFormat("<form name='form' action='{0}' method='post'>", access_token_uri);
sb.Append("<input type='hidden' name='grant_type' value='authorization_code'>");
sb.AppendFormat("<input type='hidden' name='client_id' value='{0}'>", mailchimp_clientid2);
sb.AppendFormat("<input type='hidden' name='client_secret' value='{0}'>", mailchimp_secret2);
sb.AppendFormat("<input type='hidden' name='code' value='{0}'>", code);
sb.AppendFormat("<input type='hidden' name='redirect_uri' value='{0}'>", "http://127.0.0.1:18017/Home/AuthComplete");
// Other params go here
sb.Append("</form>");
sb.Append("</body>");
sb.Append("</html>");
Response.Write(sb.ToString());
Response.End();
return null;
}
public ActionResult AuthComplete(string access_token, string expires_in, string scope)
{
if (string.IsNullOrWhiteSpace(access_token))
throw new Exception("Could not authorize user with MailChimp");
var user = (Mailchimp_users)Session["user"];
user.MailChimpToken = access_token;
EcoSession.DataSession.Refresh(System.Data.Objects.RefreshMode.ClientWins, user);
EcoSession.DataSession.SaveChanges();
return RedirectToAction("Index", "Subscribe");
}
}
It is step 4 that is killing me, not step 5.
You should send your request parameters in the body using post, if you were using curl php you would do this:
$value = http_build_query($params); //params is an array
curl_setopt($ch, CURLOPT_POSTFIELDS, $value);
Value should look like this:
grant_type=authorization_code&client_id=635959587059&client_secret=0da3e7744949e1406b7b250051ee1a95&code=1edf2589e664fd317f6a7ff5f97b42f7&redirect_uri=http%3A%2F%2F192.168.1.8%2Foauth%2Fcomplete.php
Notice that you should create a body request in the form a query string, don't send json, they wont't find your params.
If you get an invalid grant response or something after doing this, check that the redirect uri you used to get the first code is EXACTLY the same as the one you are sending to get the token.
Also, to the ones using PHP, to match what the mailchimp documentation states use this:
curl_setopt($ch, CURLOPT_USERAGENT, 'oauth2-draft-v10');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
If you don't want to implement it yourself, there is this nice wrapper, which handles oAuth2 AND RESTapi-calls to MailChimp.
https://github.com/jamierytlewski/eepOAuth2-MVC
Step 4 is "Your application must make an out-of-band request to the access_token_uri using the code"
The main point here is "out of band".
You have to build and send a post request server-side.
The client should not have your mailchimp_secret
Your IncomingMailChimpToken could look like this :
public ActionResult IncomingMailChimpToken(string code)
{
string mcPostData = String.Format(
"grant_type={0}&client_id={1}&client_secret={2}&code={3}&redirect_url={4}",
System.Web.HttpUtility.UrlEncode("authorization_code"),
System.Web.HttpUtility.UrlEncode(mailchimp_clientid2),
System.Web.HttpUtility.UrlEncode(mailchimp_secret2),
System.Web.HttpUtility.UrlEncode(code),
System.Web.HttpUtility.UrlEncode("http://127.0.0.1:18017/Home/AuthComplete")
);
WebRequest request = WebRequest.Create(access_token_uri);
// Set the Method property of the request to POST.
request.Method = "POST";
request.ContentType = "application/json";
byte[] byteArray = Encoding.UTF8.GetBytes(mcPostData);
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse();
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Cleanup the streams and the response.
reader.Close ();
dataStream.Close ();
response.Close ();
// parse the json responseFromServer to extract token, expires_in and scope
// and call AuthComplete with these params
}

Resources