2017-05-17 160 views
0

當用戶創建新應用程序時,我還需要從AWS生成API密鑰。我需要使用特定的使用計劃(靜態)來分配給每個新客戶端。我收到錯誤params [:usage_plan_id]導致救援的意外值。任何想法,我在這裏失蹤?使用計劃創建AWS API網關客戶端API密鑰

def create 
    @app = App.create(app_params) 

    begin 

    # ... 

    aws_client = Aws::APIGateway::Client.new(
     region: "xxxxxxxxxxx", 
     access_key_id: AWS_ACCESS_KEY, 
     secret_access_key: AWS_ACCESS_SECRET 
    ).create_api_key({ 
     name: @app.name, 
     enabled: true, 
     generate_distinct_id: true, 
     usage_plan_id: "xxxxxx" 
    }) 

    logger.debug aws_client.inspect 

    rescue => e 
    logger.error e.message 
    # ... 
    end 

    respond_with(@app) 
end 

回答

0

這裏是需要採取什麼措施:

  1. 建立新的客戶端連接到AWS API網關。
  2. 爲客戶端創建API密鑰。
  3. 將api_key客戶端添加到使用計劃。

    API_GATEWAY = Aws::APIGateway::Client.new(
        region: "xxxxx", 
        access_key_id: "xxxxxxxxxxxxxxxx", 
        secret_access_key: "xxxxxxxxxxxxxx" 
    ) 
    

    在控制器:

    aws_api_key = API_GATEWAY.create_api_key({ 
        name: @app.name, 
        enabled: true, 
        generate_distinct_id: true, 
        stage_keys: [{ 
        rest_api_id: "xxxxxxx", 
        stage_name: "xxxxxxxx" 
        }] 
    }) 
    
    resp = API_GATEWAY.create_usage_plan_key({ 
        usage_plan_id: "xxxxxx", 
        key_id: aws_api_key.id, 
        key_type: "API_KEY" 
    }) 
    
在AWS配置文件

相關問題