如何使用iOS生成的SDK for AWS API Gateway(Swift)

时间:2015-09-02 20:42:57

标签: ios swift sdk aws-api-gateway

我已使用AWS API Gateway创建了API,但每当我尝试访问任何资源时,都会收到此错误。

Error Domain=com.amazonaws.AWSAPIGatewayErrorDomain Code=1 "(null)" UserInfo={HTTPBody=<CFBasicHash 0x7fd5d87baaf0 [0x102c007b0]>{type = immutable dict, count = 1,
entries =>
0 : message = <CFString 0x7fd5d87bf100 [0x102c007b0]>{contents = "Not able to access resource."}
}
, HTTPHeaderFields=<CFBasicHash 0x7fd5d87bf2a0 [0x102c007b0]>{type = immutable dict, count = 8,
entries =>
0 : X-Cache = <CFString 0x7fd5d87bf030 [0x102c007b0]>{contents = "Error from cloudfront"}
1 : Content-Type = <CFString 0x7fd5d87bd900 [0x102c007b0]>{contents = "application/json"}
3 : x-amzn-RequestId = <CFString 0x7fd5d87baa70 [0x102c007b0]>{contents = "2728fe5f-51b0-11e5-8d25-ada09d3fa091"}
4 : Via = <CFString 0x7fd5d87bcfb0 [0x102c007b0]>{contents = "1.1 87b90692aeeb296771124f5335f08b68.cloudfront.net (CloudFront)"}
6 : Date = <CFString 0x7fd5d87be8d0 [0x102c007b0]>{contents = "Wed, 02 Sep 2015 20:21:11 GMT"}
10 : Content-Length = 43
11 : X-Amz-Cf-Id = <CFString 0x7fd5d87bd0f0 [0x102c007b0]>{contents = "0aL_H-QzchVHBgsYChJ9YwJOs0PKpdFzouXJsDDausqQ9bpunaJmFg=="}
12 : Connection = <CFString 0x7fd5d87be8b0 [0x102c007b0]>{contents = "keep-alive"}
}
}

我的资源需要api密钥,但没有&#34;授权类型&#34;。

这是我配置客户端的AppDelegate.swift的片段:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: nil)
    AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
    MYREKSMyreksApiClient.registerClientWithConfiguration(configuration, forKey: "avfhh3FNkh4JDGWG8AHsH4ogucFZjmkEUJIn0C44")
    return true
}

这就是我从控制器那里打来的电话。

let serviceClient = CLIMyApiClient(forKey: apiKey)
    var object = CLICustomObject()
    object.param_01 = "Param 01"
    object.param_02 = "Param 02"
    let result = serviceClient.customObjectPost(contentShare).continueWithBlock{ (task:AWSTask!) -> (AnyObject!) in
        if task.error != nil {
            print(task.error)
        } else {
            print("Good!")
        }
        return nil
    }

为什么我得到那个错误的想法?

1 个答案:

答案 0 :(得分:2)

好的,我已设法识别错误......

我的代码中出现了一些错误:

  1. 当我注册我的客户端&#34; forKey&#34;时,该密钥只​​是一个Dict密钥来检索我的客户端。我当时并没有真正添加我的API密钥。所以,我把它改成了一个更有意义的钥匙。

  2. 要在客户端中实际设置我的API密钥,我必须......好吧,在客户端设置它:

    let serviceClient = CLIApiClient(forKey: "MoreMeaningfulKey")
    serviceClient.APIKey = "MyActuallyAPIKey"
    
  3. 那就是它!

    显然,在AWS API Gateway,iOS和Android团队中,他们不会互相交流。这就是你在Android上调用它的方式:

    ApiClientFactory factory = new ApiClientFactory().endpoint(END_POINT).apiKey(API_KEY);
    MyApiClient client = factory.build(MyApiClient.class);