1

我正在尝试使用 iPhone 授权 LinkedIn ..

我正在使用以下代码重定向 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]];

在我的 Url 类型中,我添加了 URL Scheme ashttp://和 url identifier as

 www.myappname.com

但是授权后,我不会从浏览器返回我的应用程序。

有什么想法我错了吗?

4

2 回答 2

3

我现在使用了差异方法,我在应用程序中使用了 webView 并且正在使用它。到目前为止工作完美。

- (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;
}
于 2013-04-21T10:59:37.607 回答
2

您将需要注册一个自定义 URL 方案,因为 iOS 和 OS X 无法在 URL 方案中重定向特定主机。

通常,您可以使用类似x-myapp:url 方案的东西。

此外,URL 标识符不是主机,而是描述 URL 方案的标识符,很像文件类型的 UTI 标识符描述特定文件类型。例如,您可以使用com.myCompany.myApp.url或类似的东西作为标识符。

x-myapp:如果您创建一个表单方案然后将其用作重定向 URL,您应该没问题。

提议的 Info.plist 中的一个示例是:

<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>

CFBundleURLNameXcode GUI 中对应于URL Identifier.

于 2013-04-04T16:31:47.900 回答