我想为 iOS 应用程序使用 Google JSON API。但我会在没有用户身份验证的情况下这样做,因为不会存储任何敏感数据。所以我希望使用此处描述的公共(public) API key https://cloud.google.com/storage/docs/json_api/v1/how-tos/authorizing
请求地址如下:
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");
}
使用这个,我得到一个 401 Login Required Error。
我错过了什么?
最佳答案
您正在尝试将对象匿名上传到存储桶中。默认情况下不允许这样做,因为这意味着任何知道您的存储桶的人都可以无限制地将任意数量的对象上传到您的存储桶中。
不过,如果您对这种可能性感到满意,那当然是有可能的。授予所有用户对存储桶的 WRITE 权限将使您问题中的调用成功。
关于ios - 不使用 Oauth 的 Google Json Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30119869/