AFOAuth2Client和Stack Exchange OAuth2身份验证

时间:2013-08-01 17:19:26

标签: ios oauth oauth-2.0 afnetworking afoauth2client

我正在尝试在我的某个应用中使用Stack Exchange API。我发现似乎AFOAuth2Client框架就是这样做的。

我已经导入了AFNetworking,就像入门文档所说的那样,它似乎正在起作用。我在我的应用程序中有这个代码:

NSURL *url = [NSURL URLWithString:@"https://stackexchange.com/"];
AFOAuth2Client *oauthClient = [AFOAuth2Client clientWithBaseURL:url clientID:@"1xxx" secret:@"x3xxWxxvxxSxxexx0xx2Vxx("];

[oauthClient authenticateUsingOAuthWithPath:@"/oauth/dialog"
                                   username:@"username"
                                   password:@"password"
                                      scope:@"email"
                                    success:^(AFOAuthCredential *credential) {
                                        NSLog(@"I have a token! %@", credential.accessToken);
                                        [AFOAuthCredential storeCredential:credential withIdentifier:oauthClient.serviceProviderIdentifier];
                                    }
                                    failure:^(NSError *error) {
                                        NSLog(@"Error: %@", error);
                                    }];

但是,我从未被要求通过SE网站登录,并将其打印到控制台:

StackBoard[1155:907] Error: Error Domain=AFNetworkingErrorDomain Code=-1016 "Expected content type {(
    "text/json",
    "application/json",
    "text/javascript"
)}, got text/html" UserInfo=0x1fdf6fb0 {NSLocalizedRecoverySuggestion=







<!DOCTYPE html>

<html lang="en">

<head>

    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />

    <title>

    Application Login Failure - Stack Exchange

</title>

    <link rel="stylesheet" href="https://cdn.sstatic.net/stackexchange/all.css?v=7b99cfb7f47f" type="text/css" />

    <link rel="shortcut icon" href="https://cdn.sstatic.net/stackexchange/img/favicon.ico">

    <link rel="apple-touch-icon" href="https://cdn.sstatic.net/stackexchange/img/apple-touch-icon.png">

    <link rel="search" type="application/opensearchdescription+xml" title="Stack Exchange" href="/opensearch.xml">



    <!--[if IE]>

    <script src="https://cdn.sstatic.net/stackexchange/js/third-party/html5shiv.js?v=9f4cac0f1832"></script>

    <![endif]-->

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" ></script>

    <script src="https://cdn.sstatic.net/stackexchange/js/third-party/jquery.cookie.js?v=bafd924fa942"></script>

    <script src="https://cdn.sstatic.net/stackexchange/js/master.js?v=153a719c336e"></script>



    <script type="text/javascript">

        $(function () {

            genuwine.init(undefined);

        });

        var imagePath='https://cdn.sstatic.net/stackexchange/Img/';

        var inboxUnviewedCount = -1;

    </script>

        <script src="https://cdn.sstatic.net/stackexchange/js/global-login.js?v=34fe03400da2"></script>



    <script type="text/javascript">

        $(function () { $('.show-error').click(function () { $(this).hide(); $('.error-details').attr('style', ''); }); });

    </script>



</head>

<body>

    <noscript><div id="noscript-padding"></div></noscript>

    <div class="wrapper">

        <div id="overlay-header"></div>

        <section id="topBar">

            <div class="contentWrapper">

                <div id="portalLink">

                    <a class="genu" href="http://stackexchange.com">Stack Exchange</a>            

                </div>

                <div class="search">

                    <form action="/search">

                        <input id="search" class="search-box" type="text" name="q" size="31" title="search all sites"/>

                    </form>

                </div>



                <nav id="top">

    <ul>

        <li>

                    <a href="/users/login?returnurl=%2foauth%2fdialog">log in</a>

        </li>

        <li><a href="http://chat.stackexchange.com/">chat</a></li>

        <li><a href="http://meta.stackoverflow.com">meta</a></li>

        <li><a href="/about">about</a></li>

    </ul>

</nav>

            </div>

        </section>



        <header>

            <div class="contentWrapper">

                <a href="/"><h1>Stack Exchange</h1></a>



                <ul class="navMain">

                    <li><a href="/sites" >All Sites</a></li>

                    <li><a href="/questions" >Hot Questions</a></li>

                    <li><a href="/leagues" >Top Users</a></li>

                    <li><a href="/newsletters" >Newsletters</a></li>

                    <li><a href="/blogs" >Blogs</a></li>

                </ul>

            </div>

        </header>









        <section id="content">

            <div class="contentWrapper">





<div>

    <h2>Application Login Failure</h2>



    <p>An error occurred while login into an application.</p>



    <a href="#" class="show-error">Show Error Details</a>



    <div class="error-details" style="display:none">

        <h3>Error Details</h3>



        <p>





            <b>error description:</b> <span>application not configured for implicit grants</span><br>



        </p>

    </div>

</div>

            </div>

        </section>



        <script type="text/javascript">

            var _gaq = _gaq || [];

            _gaq.push(['_setAccount', 'UA-5620270-24']);

            _gaq.push(['_setDomainName', '.stackexchange.com']);

            _gaq.push(['_trackPageview']);



            (function () {

                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;

                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';

                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

            })();



            _qoptions = { qacct: "p-c1rF4kxgLUzNc" };

        </script>





    </div> <!-- end wrapper -->



    <footer id="footer">

        <div class="contentWrapper">

            <div class="footerLinksSpaced">

                <p>

                    <a href="/about">about</a> <a href="http://blog.stackexchange.com">blog</a> <a href="/legal">legal</a> <a href="/legal/privacy-policy">privacy policy</a> <a href="/about/contact">contact us</a> <a href="http://meta.stackoverflow.com">feedback always welcome</a>

                    <a href="https://plus.google.com/+StackExchange" class="google-plus-icon" title="follow us on Google+"></a>

                    <a href="http://www.facebook.com/stackexchange" class="facebook-icon" title="like us on Facebook"></a>

                    <a href="http://twitter.com/stackexchange" class="twitter-icon" title="follow us on Twitter"></a>

                </p>

            </div>

            <p>site design / logo &copy; 2013 stack exchange, inc; user contributions licensed under <a href="http://creativecommons.org/licenses/by-sa/3.0/" rel="license">cc-wiki</a> with <a href="http://blog.stackoverflow.com/2009/06/attribution-required/" rel="license">attribution required</a></p>

            <div class="footer-meta-info">

                2013.7.29.644

            </div>



        </div>

    </footer>







    <noscript>

        <div id="noscript-warning">Stack Exchange works best with JavaScript enabled</div>

    </noscript>

    <script type="text/javascript">

        $('#search')

            .focus(function() { 

                if (this.value == this.title) {

                    this.value = '';

                    $(this).removeClass('search-box-blur');

                }

            })

            .blur(function() {

                if (this.value == '') {

                    this.value = this.title;

                    $(this).addClass('search-box-blur');

                }

            })

            .blur();

        settings = {};    

        settings.contentPath = "https://cdn.sstatic.net/stackexchange";

    </script>





</body>

</html>

, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest https://stackexchange.com/oauth/dialog>, NSErrorFailingURLKey=https://stackexchange.com/oauth/dialog, NSLocalizedDescription=Expected content type {(
    "text/json",
    "application/json",
    "text/javascript"
)}, got text/html, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x1fd49700>}

我做错了什么?更重要的是,我该如何解决?

0 个答案:

没有答案