Question

I took up development in Objective C recently and I'm trying to use the Microsoft Translator APIs. So far, it's working if I use an appID, but that method is now deprecated. I'm trying to learn it the proper way using OAuth, etc.

Could anyone give me a sample (obviously without the clientID and the client_secretcode, both of which I have) to get the access_token?

Edit : seems I haven't been clear enough so there's the full story.

I've been trying to use the Microsoft Translator APIs. At the time of the question, I had found an old implementation which was using an AppID (now deprecated). MS Translator APIs are using OAuth for authentication and to be fair, at the time I had no idea how to approach it. Now, I do and I posted my code so that people could get it if they get the same problem.

Hope it's clearer now :)

Was it helpful?

Solution

Here is the sample I can up with:

-(IBAction)getToken:(id)sender {
NSString *clientSecret = (__bridge NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                                      (CFStringRef)CLIENT_SECRETCODE,
                                                                                      NULL,
                                                                                      (CFStringRef) @"!*'();:@&=+$,/?%#[]",
                                                                                      kCFStringEncodingUTF8);
NSMutableString* authHeader = [NSMutableString stringWithString:@"client_id="];
[authHeader appendString:CLIENT_ID];
[authHeader appendString:@"&client_secret="];
[authHeader appendString:clientSecret];
[authHeader appendString:@"&grant_type=client_credentials&scope=http://api.microsofttranslator.com"];



NSMutableURLRequest *request =[NSMutableURLRequest
                               requestWithURL:[NSURL URLWithString:TOKEN_STRING]
                               cachePolicy:NSURLRequestUseProtocolCachePolicy
                               timeoutInterval:60.0];

[request setHTTPMethod:@"POST"];
[request addValue:@"application/x-www-form-urlencoded"
         forHTTPHeaderField:@"Content-Type"];

const char *bytes = [authHeader UTF8String];
[request setHTTPBody:[NSData dataWithBytes:bytes length:strlen(bytes)]];

NSURLResponse* response; 
NSError* error;

NSData* data = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];

if (data != nil) {
    NSString* contents = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
    NSString *formattedContents = [contents stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
}

Even though it's adapted for the Microsoft Translation, it shouldn't be difficult to adapt it for other services. Here, the token is not parsed, the code is rough and since I'm using automatic reference counting, it should be noted that autorelease may be needed following your configuration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top