gpt4 book ai didi

ios - 如何在此代码中使用 CHQueryStringFromParametersWithEncoding

转载 作者:行者123 更新时间:2023-11-28 18:31:01 25 4
gpt4 key购买 nike

  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://titusandbindu.com/bm/wc-api/v2/customers"]];
request.HTTPMethod = @"GET";
static NSString *key=@"ck_3045dfb6122e50a79b1a35268078c4bc";
static NSString *secret=@"cs_a7ac11bc25431b1234377d4c30aed8ce";
static NSString *HMAC_SHA1=@"HmacSHA1";
NSString *timestamp=[NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] * 1000];
NSString *nonce=[NSString stringWithFormat:@"%d",(int)arc4random() * 100000000];
NSLog(@"key:%@,secret:%@,timestamp:%@",key,secret,timestamp);
NSLog(@"%@",nonce);


NSMutableDictionary *jsonpassdict=[[NSMutableDictionary alloc]init];
[jsonpassdict setValue:key forKey:@"oauth_consumer_key"];
[jsonpassdict setValue:HMAC_SHA1 forKey:@"oauth_signature_method"];
[jsonpassdict setValue:timestamp forKey:@"oauth_timestamp"];
[jsonpassdict setValue:nonce forKey:@"oauth_nonce"];

错误是:

 NSString *parametersString = [NSString stringWithFormat:@"%d",CHQueryStringFromParametersWithEncoding(jsonpassdict , NSUTF8StringEncoding)];
NSLog(@"%@",parametersString);
//NSString *parametersString = CHQueryStringFromParametersWithEncoding(jsonpassdict, NSUTF8StringEncoding);

最佳答案

OAuth Authentication in iOS
===========================
# OAuth Authentication in iOS
responseData = [[NSMutableData alloc]init];
NSString *httpMethod = @"POST";
NSString *baseURL = @""; // Server Url
NSString *oauthConsumerKey = @"ck_3045dfb6122e50a79b1a35268078c4bc";
NSString *oauthConsumerSecret = @"cs_a7ac11bc25431b1234377d4c30aed8ce";
NSString *oauth_timestamp = [NSString stringWithFormat:@"%.f", [[NSDate date]timeIntervalSince1970]];
NSString *oauthNonce = [self generateRandomStringOfLength:42];
NSString *oauthSignatureMethod = @"HMAC-SHA1";

static NSString *username=@""; // User Name
static NSString *password=@""; // Password

NSMutableString *parameterString = [[NSMutableString alloc]initWithFormat:@""];


[parameterString appendFormat:@"&oauth_consumer_key=%@", [oauthConsumerKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[parameterString appendFormat:@"&oauth_nonce=%@", [oauthNonce stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[parameterString appendFormat:@"&oauth_signature_method=%@", [oauthSignatureMethod stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[parameterString appendFormat:@"&oauth_timestamp=%@", [oauth_timestamp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSString *authStr = [NSString stringWithFormat:@"%@:%@", username, password];
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding];

NSString *authValue = [NSString stringWithFormat:@"Basic %@",[authData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed]];
[request setValue:authValue forHTTPHeaderField:@"Authorization"];

NSString *signatureBaseString = [NSString stringWithFormat:@"%@&%@&%@", httpMethod, [baseURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [parameterString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSString *signingKey = [NSString stringWithFormat:@"%@&", [oauthConsumerSecret stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

NSString *oauthSignature = [self hmacsha1:signatureBaseString secret:signingKey];


NSMutableString *authHeader=[NSMutableString stringWithFormat:@"%@?oauth_consumer_key=%@&oauth_signature_method=%@&oauth_timestamp=%@&oauth_nonce=%s&oauth_signature=%@",[baseURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[oauthConsumerKey stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[oauthSignatureMethod stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[oauth_timestamp stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],[oauthNonce UTF8String],[oauthSignature stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"%@",authHeader);

NSString *email=self.mailTextFiled.text;
NSString *Username=self.nameTextField.text;
NSString *Password=self.passwordTextField.text;



NSDictionary *customer=@{@"customer" : @{@"email": email,
@"password" : Password,
@"first_name": @"",
@"last_name": @"",
@"username": Username,
@"billing_address": @{ },
@"shipping_address": @{ }
}
};
NSMutableDictionary *customers=[customer mutableCopy];
NSLog(@"%@",customers);

NSError *jsonSerializationError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:customers options:kNilOptions error:&jsonSerializationError];

if(!jsonSerializationError) {
NSString *serJSON = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"Serialized JSON: %@", serJSON);
} else {
NSLog(@"JSON Encoding Failed: %@", [jsonSerializationError localizedDescription]);
}
request = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:authHeader]] ;
//[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; // 2
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[jsonData length]] forHTTPHeaderField:@"Content-Length"]; // 3
[request setHTTPBody: jsonData];


[request setHTTPMethod:httpMethod];

[request setValue:authHeader forHTTPHeaderField:@"Authorization"];

connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

[connection start];

关于ios - 如何在此代码中使用 CHQueryStringFromParametersWithEncoding,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30371648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com