1

I am trying to authorize LinkedIn with iPhone..

I am using following code to redirect url

NSString *authUrl = [NSString stringWithFormat:@"https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=%@&scope=%@&state=%@&redirect_uri=%@" ,
                     API_KEY ,
                     @"r_fullprofile",
                     @"ASDKASIIWER23432KKQ",
                     @"http://www.myappname.com"
                     ];


[[UIApplication sharedApplication] openURL:[NSURL URLWithString: authUrl]];

In my Url types, i have added URL Scheme as http:// and url identifier as

 www.myappname.com

however after authorizing , i don't get back to my application from browser.

Any ideas where i am wrong?

Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
  • I am facing the same issue but using the webView is not the right answer to this question. because oAuth is for not providing users credentials to the third party application(mobile app in our case) but If I am using the webview I can write a script to get users credentials. that's a potential security issue. any root solution for this issue ? – er.vish May 29 '15 at 12:59

2 Answers2

3

i have used a diff approach now, i have used webView inside app and is using it. Works perfect so far.

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.myWebView setDelegate:self];
    self.indicator = [[CustomActivityViewer alloc] initWithView:self.view];

    NSString *authUrl = [NSString stringWithFormat:@"https://www.linkedin.com/uas/oauth2/authorization?response_type=code&client_id=%@&scope=%@&state=%@&redirect_uri=%@" ,
                         API_KEY ,
                         @"r_fullprofile rw_nus r_emailaddress r_network w_messages",
                         SCOPE_CODE
                         REDIRECT_URI
                         ];
    authUrl = [authUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:authUrl]]];
}

-(void)webViewDidStartLoad:(UIWebView *)webView
{
    [self.indicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [self.indicator stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    [self.indicator stopAnimating];
}

- (BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType
{
    NSURL *url = request.URL;
    NSLog(@"%@", url.absoluteString);

    if ( [url.host isEqualToString:HOST])
    {
        URLParser *parser = [[URLParser alloc] initWithURLString:url.absoluteString];
        NSString *code = [parser valueForVariable:@"code"];

        if (code != nil)
        {
            NSString *authUrl = [NSString stringWithFormat:@"https://www.linkedin.com/uas/oauth2/accessToken?grant_type=authorization_code&code=%@&redirect_uri=%@&client_id=%@&client_secret=%@",
                                 code,
                                 REDIRECT_URI_OAUTH,
                                 API_KEY,
                                 SECRET_KEY];

            NSLog(@"%@" , authUrl);
            authUrl = [authUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

           [Utilities responseFromURL:[NSURL URLWithString:authUrl] completionBlock:^(NSString *response, NSError *err)
            {
                if (err != nil)
                {
                    [Utilities errorDisplay];
                }
                else
                {
                    NSDictionary *results = [response JSONValue];
                    [defaults setObject:[results objectForKey:@"access_token"] forKey:@"access_token"];
                }
           }];
        }
    }
    return YES;
}
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
2

You are going to need to register a custom URL scheme, as iOS and OS X don't have a way to redirect a specific host within a URL scheme.

Generally, you can use something like x-myapp: as the url scheme.

Further, the URL Identifier is not a host, but an identifier that describes the URL scheme, much like a UTI identifier for a file type describes a specific file type. For example, you could use com.myCompany.myApp.url or something similar as the identifier.

You should be fine if you create a scheme of form x-myapp: and then use that as the redirect URL.

An example from a proposed Info.plist would be:

<dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLName</key>
    <string>com.myCompany.myApp.url</string>
    <key>CFBundleURLSchemes</key>
    <array>
        <string>x-myapp</string>
    </array>
</dict>

The CFBundleURLName corresponds in the Xcode GUI to URL Identifier.

gaige
  • 17,263
  • 6
  • 57
  • 68
  • can you give example. i have to pass a redirect url, lets say it is my app://myappname.com what should i set as url scheme and identifier? – Muhammad Umar Apr 04 '13 at 16:36
  • I think this is mostly above, you'd set the url scheme to something like `x-myapp:` and the identifier to `com.mycompany.myapp.url` and then pass the url `x-myapp:` as the redirect URL. The system will make sure that your app is launched whenever a url beginning with that scheme is opened. – gaige Apr 04 '13 at 16:43
  • linkedin api wants you to send http or https only unfortunately – Muhammad Umar Apr 04 '13 at 16:48
  • Inconvenient. Well, you can always trampoline off of a link on your web site to the other URL scheme. Basically set up a page on your site which does a 301 redirect to `x-myapp:`. – gaige Apr 04 '13 at 16:51
  • If you don't have a web site, that's a different problem. Can you even sign up as a developer and ship an App without one these days? – gaige Apr 04 '13 at 18:54
  • i have used a diff approach now, i have used webView inside app and is using it. Works perfect so far. – Muhammad Umar Apr 05 '13 at 11:14