0

我正在构建一个 iOS 应用程序,客户可以在其中登录(使用相同的网站用户名和密码)到该应用程序并从该应用程序购买商品。woo commerce 的其余 api 表示通过提供 API Consumer Key 作为用户名和 API Consumer Secret 作为密码来使用 HTTP Basic Auth。我一直在研究很多,但我真的无处可去。现在我正在尝试 AFOAuth2manager,这是我的代码,我到目前为止没有工作

NSURL *baseURL = [NSURL URLWithString:urlString];
AFOAuth2Manager *oAuthManager = [[AFOAuth2Manager alloc] initWithBaseURL:baseURL
                                                                clientID: keyClientID
                                                                  secret:keyClientSecret];

[oAuthManager authenticateUsingOAuthWithURLString:@"/oauth/token"
                                         username:myUserName
                                         password:myPassword
                                            scope:@"email"
                                          success:^(AFOAuthCredential *credential) {
                                              NSLog(@"Token: %@",credential.accessToken);
                                          } failure:^(NSError *error) {
                                              NSLog(@"Error: %@", error);
                                          }];


//Authorizing requests
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];

[manager.requestSerializer setAuthorizationHeaderFieldWithUsername:keyClientID password:keyClientSecret];

[manager GET:@"/path/to/protected/resource"
  parameters:nil
     success:^(AFHTTPRequestOperation *operation, id responseObject) {
         NSLog(@"Succes: %@", responseObject);
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"Failure: %@", error);
     }];

//storing credentials
AFOAuthCredential *credential = [[AFOAuthCredential alloc] init];

[AFOAuthCredential storeCredential: credential withIdentifier:serviceProviderIdentifier];

//retrieving credentials
AFOAuthCredential *storedCredential = [AFOAuthCredential retrieveCredentialWithIdentifier:serviceProviderIdentifier];
NSLog(@"%@", storedCredential);

我收到此错误:NSLocalizedDescription=Request failed: not found (404),以及一长串数字。无论如何,我正在寻找有关该过程的一些指南,我需要让用户登录我的应用程序,我的应用程序确认用户在网站上有一个帐户,用户可以在我的应用程序上购买商品,然后我将订单发布到网站进行处理。任何帮助是极大的赞赏

4

1 回答 1

0

尝试这个

NSURL *baseURL = [NSURL URLWithString:urlString];
AFOAuth2Manager *oAuthManager = [[AFOAuth2Manager alloc] initWithBaseURL:baseURL
                                                                客户ID:keyClientID
                                                                  秘密:keyClientSecret];

[oAuthManager authenticateUsingOAuthWithURLString:@"/oauth/token"
                                         用户名:我的用户名
                                         密码:我的密码
                                            范围:@“电子邮件”
                                          成功:^(AFOAuthCredential *credential){
                                              NSLog(@"Token: %@",cr​​edential.accessToken);

//授权请求
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];

[manager.requestSerializer setAuthorizationHeaderFieldWithCredential:authCredential];

[经理 GET:@"/path/to/protected/resource"
  参数:无
     成功:^(AFHTTPRequestOperation *操作,id responseObject){
         NSLog(@"成功:%@", responseObject);
     } 失败:^(AFHTTPRequestOperation *操作,NSError *错误){
         NSLog(@"失败:%@", 错误);
     }];

                                          } 失败:^(NSError *error){
                                              NSLog(@"错误:%@", 错误);
                                          }];

...用于测试机制!

于 2015-06-16T09:54:46.607 回答