Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS+;gtm-oauth2库:获取401无效请求_Ios_Objective C_Ruby On Rails_Oauth - Fatal编程技术网

iOS+;gtm-oauth2库:获取401无效请求

iOS+;gtm-oauth2库:获取401无效请求,ios,objective-c,ruby-on-rails,oauth,Ios,Objective C,Ruby On Rails,Oauth,我有一个RubyonRails应用程序,它是一个OAuth2.0提供者,多亏了Doorkeeper gem,并且有一个API。我正在构建一个与API交互的iPhone客户端;它使用gtm-oauth2库进行身份验证过程 我完成了OAuth身份验证部分,因此用户可以向iPhone应用程序添加帐户。现在,我需要从iPhone(或iphonesimulator)密钥链检索授权,以便与我的主应用程序的私有API交互。以下是我的代码: GTMOAuth2Authentication *auth = [_d

我有一个RubyonRails应用程序,它是一个OAuth2.0提供者,多亏了Doorkeeper gem,并且有一个API。我正在构建一个与API交互的iPhone客户端;它使用gtm-oauth2库进行身份验证过程

我完成了OAuth身份验证部分,因此用户可以向iPhone应用程序添加帐户。现在,我需要从iPhone(或iphonesimulator)密钥链检索授权,以便与我的主应用程序的私有API交互。以下是我的代码:

GTMOAuth2Authentication *auth = [_db catapultAuthenticaiton];
[GTMOAuth2ViewControllerTouch authorizeFromKeychainForName:kCatapultKeychainItemName
                                            authentication:auth];

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.lvh.me:3000/api/clients/%@", accountName]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
GTMHTTPFetcher *fetcher = [GTMHTTPFetcher fetcherWithRequest:request];
[fetcher setAuthorizer:auth];
[fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
    if (error != nil) {
#if DEBUG
        NSLog(@"Error fetching client: %@", error);
#endif
    }
}];
在fetcher completion handler中,我在尝试从keychain检索现有授权并对服务器执行API调用时遇到401 invalid_请求错误:

2013-04-02 11:08:55.546 Catapult for iOS[31776:c07] Error Error Domain=com.google.HTTPStatus Code=401 "The operation couldn’t be completed. (com.google.HTTPStatus error 401.)" UserInfo=0x7248010 {data=<7b226572 726f7222 3a22696e 76616c69 645f7265 71756573 74222c22 6572726f 725f6465 73637269 7074696f 6e223a22 54686520 72657175 65737420 6973206d 69737369 6e672061 20726571 75697265 64207061 72616d65 7465722c 20696e63 6c756465 7320616e 20756e73 7570706f 72746564 20706172 616d6574 65722076 616c7565 2c206f72 20697320 6f746865 72776973 65206d61 6c666f72 6d65642e 227d>}
Error data:
{
    error = "invalid_request";
    "error_description" = "The request is missing a required parameter, includes an unsupported parameter value, or is otherwise malformed.";
}
2013-04-02 11:08:55.546 Catapult for iOS[31776:c07] Error fetching client: Error Domain=com.google.HTTPStatus Code=401 "The operation couldn’t be completed. (com.google.HTTPStatus error 401.)" UserInfo=0x725bf00 {data=<7b226572 726f7222 3a22696e 76616c69 645f7265 71756573 74222c22 6572726f 725f6465 73637269 7074696f 6e223a22 54686520 72657175 65737420 6973206d 69737369 6e672061 20726571 75697265 64207061 72616d65 7465722c 20696e63 6c756465 7320616e 20756e73 7570706f 72746564 20706172 616d6574 65722076 616c7565 2c206f72 20697320 6f746865 72776973 65206d61 6c666f72 6d65642e 227d>, json={
    error = "invalid_request";
    "error_description" = "The request is missing a required parameter, includes an unsupported parameter value, or is otherwise malformed.";
}}
以下是服务器的日志:

Started POST "/oauth/token" for 127.0.0.1 at 2013-04-02 11:08:55 +0100
  Doorkeeper::AccessToken Load (0.2ms)  SELECT "oauth_access_tokens".* FROM "oauth_access_tokens" WHERE "oauth_access_tokens"."refresh_token" = 'ddb0a1bd3256e351ff7081300dc7ac73035e897ea93d36053b7fb87843015a51' LIMIT 1
  Doorkeeper::Application Load (0.1ms)  SELECT "oauth_applications".* FROM "oauth_applications" WHERE "oauth_applications"."uid" = 'b29a572c2caa218b8591011e1cc67f0f3536ffe4c449ab7b806dd4d363681401' AND "oauth_applications"."secret" = 'b0de316c20112708d04284f6867f5bb51c1d20c72415642b38f09a8faca966cd' LIMIT 1
当请求生效时,服务器的日志如下所示:

Started POST "/oauth/token" for 127.0.0.1 at 2013-04-02 11:19:19 +0100
  Doorkeeper::AccessGrant Load (0.2ms)  SELECT "oauth_access_grants".* FROM "oauth_access_grants" WHERE "oauth_access_grants"."token" = '4391e187a0df16c56d1e6b16bf51340af8d677dbf625c495f50a249678e0502e' LIMIT 1
  Doorkeeper::Application Load (0.1ms)  SELECT "oauth_applications".* FROM "oauth_applications" WHERE "oauth_applications"."uid" = 'b29a572c2caa218b8591011e1cc67f0f3536ffe4c449ab7b806dd4d363681401' AND "oauth_applications"."secret" = 'b0de316c20112708d04284f6867f5bb51c1d20c72415642b38f09a8faca966cd' LIMIT 1
  SQL (1.0ms)  UPDATE "oauth_access_grants" SET "revoked_at" = '2013-04-02 10:19:19' WHERE "oauth_access_grants"."id" = 7
  Doorkeeper::AccessToken Load (0.2ms)  SELECT "oauth_access_tokens".* FROM "oauth_access_tokens" WHERE "oauth_access_tokens"."application_id" = 1 AND "oauth_access_tokens"."resource_owner_id" = 266 AND "oauth_access_tokens"."revoked_at" IS NULL ORDER BY created_at desc LIMIT 1
所以这里显然有点不对劲。。。我不知道问题是来自服务器还是iOS客户端,但我的直觉告诉我它来自客户端


有什么线索吗?

您的代码没有提供足够的洞察力。我的猜测是,你有一个访问授权,你需要用它来换取一个令牌,而实际上你正试图用它来换取一个刷新令牌。
Started POST "/oauth/token" for 127.0.0.1 at 2013-04-02 11:19:19 +0100
  Doorkeeper::AccessGrant Load (0.2ms)  SELECT "oauth_access_grants".* FROM "oauth_access_grants" WHERE "oauth_access_grants"."token" = '4391e187a0df16c56d1e6b16bf51340af8d677dbf625c495f50a249678e0502e' LIMIT 1
  Doorkeeper::Application Load (0.1ms)  SELECT "oauth_applications".* FROM "oauth_applications" WHERE "oauth_applications"."uid" = 'b29a572c2caa218b8591011e1cc67f0f3536ffe4c449ab7b806dd4d363681401' AND "oauth_applications"."secret" = 'b0de316c20112708d04284f6867f5bb51c1d20c72415642b38f09a8faca966cd' LIMIT 1
  SQL (1.0ms)  UPDATE "oauth_access_grants" SET "revoked_at" = '2013-04-02 10:19:19' WHERE "oauth_access_grants"."id" = 7
  Doorkeeper::AccessToken Load (0.2ms)  SELECT "oauth_access_tokens".* FROM "oauth_access_tokens" WHERE "oauth_access_tokens"."application_id" = 1 AND "oauth_access_tokens"."resource_owner_id" = 266 AND "oauth_access_tokens"."revoked_at" IS NULL ORDER BY created_at desc LIMIT 1