ios - WooCommerce iOS OAuth 无效签名

标签 ios objective-c wordpress oauth woocommerce

当从 iOS 应用程序向 WooCommerce api 发送请求时,许多人一直在寻找解决无效签名问题的方法。

我在这里整理了解决方案。

最佳答案

从 iOS 应用程序向 WooCommerce API 发送请求时,我正在为无效签名问题提供解决方案。

要在 iOS 应用程序中使用 WooCommerce,请求必须使用 OAuth 1.0 进行身份验证。在 iOS 中,无论应用程序是用 Objective-C 还是 Swift 编写的,AFNetworking 都让编写请求时变得更轻松。

按照以下过程设置您的请求:

  • 准备好 URL、OAuth 1.0 消费者 key 和消费者 key 。
  • 使用 Cocoapods 将 AFNetworking 添加到您的项目中。您的 podfile 将如下所示:
      source 'https://github.com/CocoaPods/Specs.git'
      platform :ios, '9.0'
    
      target 'OAuthSample3' do
          use_frameworks!
    
          # Pods for OAuthSample3
          pod 'AFNetworking', '1.3.4'
      end
    

    将其保存在您的项目目录中并运行 pod install在终端。
    下一步,我们必须安装 AFNetworking 1.3.4。这就是为什么它在 podfile 中显示 1.3.4 的原因。
  • 接下来从github下载这个项目:https://github.com/khanghoang/-WooClient
    解压并复制AFOAuth1Client.h , AFOAuth1Client.m , AFOAuth1OneLeggedClient.h , AFOAuth1OneLeggedClient.m , AFOAuth1OneLeggedClientWooParser.h , AFOAuth1OneLeggedClientWooParser.m到你的项目。
  • 现在我们需要稍微修改一下这些文件。打开AFOAuth1Client.h并找到 AFHMACSHA1Signature()方法。
    寻找这些行:
      // one-legged
      if(![secret isEqualToString:@""]) {
          secretString = [secretString stringByAppendingFormat:@"&%@", AFPercentEscapedQueryStringPairMemberFromStringWithEncoding(secret, stringEncoding)];
      }  
    

    并将它们替换为:
      // one-legged  
      secretString = [secretString stringByAppendingFormat:@"&"];  
    
  • 就是这样!这将解决问题。

  • 在您的 View Controller 中,您可以像这样发出请求:
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        AFOAuth1OneLeggedClient *client = [[AFOAuth1OneLeggedClient alloc] initWithBaseURL:[NSURL URLWithString:PATH] key:OAUTH_CONSUMER_KEY secret:OAUTH_CONSUMER_SECRET];
    
        [client getPath:@"orders" parameters:@{} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    
    
    
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    
        }];
    }  
    

    您可以在字典中添加参数。 OAUTH_CONSUMER_KEYOAUTH_CONSUMER_SECRET必须替换为您在第一步中收集的值。

    你也可以在 Swift 中做到这一点。只需使用桥接头即可导入 AFNetworking 文件。

    非常感谢 khanghoang 在 AFOAuth1Client 方面的突破。

    关于ios - WooCommerce iOS OAuth 无效签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37762146/

    相关文章:

    ios - NSOperationQueue 和并发操作

    ios - SVGKit 性能以及它是否应该优于 PNG?

    ios - 当项目使用自动布局时,导航 Controller 无法在 iOS 7 中推送 View Controller

    ios - UI 测试 - 如何检查我是否在正确的 Controller 中?

    iphone - iPhone虚拟键盘添加新按钮?

    iOS 共享扩展 : need to share all the pdf files including web link pdf files through my share extension

    objective-c - Mac OSX 应用程序 - 你如何制作 "Docking window"?

    php - Wordpress Body 类未实现

    php - WordPress MySQL数据检索 酒店订房预订系统

    php - 在 Wordpress 网站上发布预加载 CSS