我想为iOS应用程序使用Google JSON API。但我会在没有用户身份验证的情况下这样做,因为不会存储敏感数据。所以我希望使用这里描述的公共API密钥https://cloud.google.com/storage/docs/json_api/v1/how-tos/authorizing
下面是请求url:
NSString* text = self.textField.text;
NSString* data = [NSString stringWithFormat:@"message=%@", text];
NSData* postData = [data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString* length = [NSString stringWithFormat:@"%lu",(unsigned long)[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
request.URL = [NSURL URLWithString:@"https://www.googleapis.com/upload/storage/v1/b/myBucket/o?uploadType=media&name=myObject&key=myApiKey"];
request.HTTPMethod = @"POST";
[request setValue:length forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection* conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
NSLog(@"connection successful");
}else {
NSLog(@"connection failed");
}使用这个tho,我得到了一个401需要登录的错误。
我遗漏了什么?
发布于 2015-05-09 12:01:20
您正在尝试将对象匿名上传到存储桶中。默认情况下,这是不允许的,因为这意味着任何知道您的存储桶的人都有可能将他们喜欢的任意数量的对象不受限制地上传到您的存储桶中。
尽管如此,如果您对这种可能性感到满意,那么它当然是可能的。授予所有用户对存储桶的写权限将允许您的问题中的调用成功。
https://stackoverflow.com/questions/30119869
复制相似问题