Phally
Phally

Reputation: 47

How do I create a post for Disqus from iOS?

I'm developing an application for a client. This app enables user to read and post comment. Our client website have already used Discus. Now that we can retrieve the comment and display it natively. But we find no way to create a post and send it to Disqus. Does anyone have some experiences with Disqus on iOS ?

Any idea will be much appreciated.

Edit : our client website has an authentication system already and thus we also integrate that system to our app. Every time user post a comment in the app, we'll use his/her authentication information. We don't want user to authenticate again.

Upvotes: 1

Views: 979

Answers (3)

Slava Todavchich
Slava Todavchich

Reputation: 269

Here is a great solution which helps easily integrate Disqus into iOS apps https://github.com/moqod/disqus-ios. Works like a charm out of the box.

Not only you can make posts, but also authorize thru social networks and respond to comments.

Upvotes: 2

Phally
Phally

Reputation: 47

@RahuGupta: Sorry if it's too late for a response. I posted comment using SSO, I don't know about posting as guest. But it should be easier.

In my case I post a comment using third party authentication server which means we authenticate users by ourselves. The technique called SSO. You may want to read the documentation about SSO on Disqus.

In case you need code snippet in objective-c :

NSMutableDictionary *dico = [[NSMutableDictionary alloc] init];
[dico setValue:[NSString stringWithFormat:@"uniqueId_%@",comment.authorID] forKey:@"id"];
[dico setValue:comment.authorName forKey:@"username"];
[dico setValue:comment.authorEmail forKey:@"email"];


NSString *message = [dico JSONRepresentation];
NSString *message64Based = [DataPost base64String:message];

NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];

NSString *secret = DISQUS_API_SECRET;
NSString *api_key = DISQUS_API_PUBLIC;

//comment.threadID will makes the app crashed, because when comment is not fully loaded, it's nil
NSString *threadID = [NSString stringWithFormat:@"%@",comment.threadID];

NSString *commentMessage = [[comment.rawMessage stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"] stringByReplacingOccurrencesOfString:@" " withString:@"+"];

NSString *host     = @"http://example.com";
NSString *referrer = @"example.com";

NSString *hmac = [self hashedValue:secret andData:[NSString stringWithFormat:@"%@ %.0lf",message64Based, timeStamp]];
NSString *remote_auth_s3 = [[[NSString stringWithFormat:@"%@ %@ %.0lf", message64Based, hmac, timeStamp] stringByReplacingOccurrencesOfString:@"=" withString:@"%3D"] stringByReplacingOccurrencesOfString:@" " withString:@"+"];


NSMutableURLRequest *uploadRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://disqus.com/api/3.0/posts/create.json"] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 60.0f];

NSMutableData *postData = (NSMutableData *)[[NSString stringWithFormat:@"_format=json&thread=%@&message=%@&remote_auth=%@&api_key=%@&strict=1", threadID , commentMessage, remote_auth_s3 , api_key] dataUsingEncoding:NSUTF8StringEncoding];
[uploadRequest setHTTPMethod:@"POST"];
[uploadRequest setHTTPBody: postData];
[uploadRequest setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[uploadRequest setValue:referrer forHTTPHeaderField:@"referrer"];
[uploadRequest setValue:host forHTTPHeaderField:@"host"];

[uploadRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

NSHTTPURLResponse *response=nil;
NSError *error = nil;
[NSURLConnection sendSynchronousRequest:uploadRequest returningResponse:&response error:&error];

Upvotes: 0

AdamG
AdamG

Reputation: 3718

You have the API set up on your server, correct? Then post a request to your server, pass it whatever API keys and credentials you need, and have the server make a post to the Disqus API on behalf of the client. Basically, build your own iOS API for interfacing with the Disqus API through your server.

Even better though would be to directly interact with the Disqus server by making requests using NSURLRequest/Connection. For more information on Disqus requests look here. That would make your app faster and be less error prone, unless you are doing some critical activity on your server, other than just posting and displaying comments.

Upvotes: 1

Related Questions