2016-06-11 63 views

回答

2

我從iOS應用程序向WooCommerce API發送請求時,爲解決無效簽名問題提供瞭解決方案。

要在iOS應用程序中使用WooCommerce,請求必須使用OAuth 1.0進行身份驗證。在iOS中,無論應用程序是使用Objective-C還是Swift編寫的,AFNetworking在編寫請求時都會讓生活更輕鬆。

按照此步驟設置你的請求:

  1. 獲取URL,1.0的OAuth消費者密鑰和消費者祕密準備。
  2. 使用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。

  3. 接下來,從GitHub下載這個項目:https://github.com/khanghoang/-WooClient
    將它解壓縮和複製AFOAuth1Client.hAFOAuth1Client.mAFOAuth1OneLeggedClient.hAFOAuth1OneLeggedClient.mAFOAuth1OneLeggedClientWooParser.hAFOAuth1OneLeggedClientWooParser.m到您的項目。

  4. 現在我們需要修改這些文件。打開AFOAuth1Client.h並找到AFHMACSHA1Signature()方法。
    查找這些行:

    // one-legged 
        if(![secret isEqualToString:@""]) { 
         secretString = [secretString stringByAppendingFormat:@"&%@", AFPercentEscapedQueryStringPairMemberFromStringWithEncoding(secret, stringEncoding)]; 
        } 
    

    ,代之以他們:

    // one-legged 
        secretString = [secretString stringByAppendingFormat:@"&"]; 
    
  5. 這就是它!這將解決問題。

在視圖控制器,你可以作出這樣的要求:

- (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上的突破。

+0

我正在尋找API呼叫以吸引商業在ios中,我用你的代碼,但得到錯誤401, {「code」:「woocommerce_rest_authentication_error」,「消息」:「無效簽名 - 提供的簽名不匹配。 –

0

添加答案@Akilan Arasu爲雨燕的解決方案「列出產品」在WooCommerce

按照前四個步驟上述溶液中,並列出產品在迅速WooCommerce 5步將

let client = AFOAuth1OneLeggedClient.init(baseURL: NSURL(string:"http://example.com/wc-api/v3/"), key: "ck_xxxxxx ",secret: "cs_xxxxx") 


    client.getPath("products", parameters: nil, success: { (operation, responseObject ) in 
     let responseArray = responseObject as? NSDictionary 
     print(responseArray) 
     let product = responseArray?.valueForKey("products") as! NSArray 

     print("JSON: " + responseObject.description) 


     }, failure: { (operation, error) in 
      print("Error: " + error.localizedDescription) 


    }) 
+0

你在橋接頭文件中導入了哪些文件?我看到一些錯誤 –

+0

@Sid我已經從https://github.com的壓縮文件中手動添加AFOAuth1Client.h,AFOAuth1Client.m,AFOAuth1OneLeggedClient.h,AFOAuth1OneLeggedClient.m,AFOAuth1OneLeggedClientWooParser.h,AFOAuth1OneLeggedClientWooParser.m這些文件/ khanghoang/-WooClient,會有AFOAuth1OneLeggedClientTests和其他測試文件在其中..因此只是忽略這些..並且還添加了#import「AFOAuth1Client.h」 #import「AFOAuth1OneLeggedClient.h」 #import「AFOAuth1OneLeggedClientWooParser.h 「在橋頭 –

+0

你有沒有更新我。 ii。橋接頭文件的搜索路徑。在構建階段將鏈接添加到鏈接二進制部分iii。搜索標題路徑。任何這些? –