当从 iOS 应用程序向 WooCommerce api 发送请求时,许多人一直在寻找解决无效签名问题的方法。
我在这里整理了解决方案。
最佳答案
从 iOS 应用程序向 WooCommerce API 发送请求时,我正在为无效签名问题提供解决方案。
要在 iOS 应用程序中使用 WooCommerce,请求必须使用 OAuth 1.0 进行身份验证。在 iOS 中,无论应用程序是用 Objective-C 还是 Swift 编写的,AFNetworking 都让编写请求时变得更轻松。
按照以下过程设置您的请求:
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 的原因。
解压并复制
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_KEY
和 OAUTH_CONSUMER_SECRET
必须替换为您在第一步中收集的值。你也可以在 Swift 中做到这一点。只需使用桥接头即可导入 AFNetworking 文件。
非常感谢 khanghoang 在 AFOAuth1Client 方面的突破。
关于ios - WooCommerce iOS OAuth 无效签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37762146/