使用API在OVH中添加新的域名区域

huangapple go评论65阅读模式
英文:

Add new Domain Zone in OVH using API

问题

我想使用OVH API和Python在OVH中添加一个新的DNS区域。

我用以下步骤编写了一个脚本:

  • 创建一个新购物车
  • 将新的DNS区域添加到购物车
  • 检查购物车内容
  • 确认订单
  • 检查订单状态

我忘记了哪个步骤,或者是有错误吗?因为当我查看GET orders时,我看不到新订单,它们也不出现在GUI中。

英文:

I want to add a new DNS zone in OVH with Python using the OVH API.

I wrote a script with these steps:

  • Create a new cart
  • Add a new DNS zone to the cart
  • Check the cart content
  • Confirm the order
  • Check the order status

Did I forget a step or is there an error somewhere? Because when I look in GET orders I don't see new orders and also they don't appear in the GUI.

cart = client.post('/order/cart', ovhSubsidiary='PL')

#Get the cart ID
cart_id = cart.get('cartId')

#Set data for the new DNS zone
zone_name = 'testttt.pl' # DNS zone name

#Add the new DNS zone to the cart
result = client.post(f'/order/cart/{cart_id}/dns',
domain=zone_name,
duration="P1Y",
planCode="zone",
pricingMode="default",
quantity=1)

#Check if the operation was successful
if 'itemId' in result:
    print(f'DNS zone {zone_name} was added to the cart.')
else:
    print('Error while adding DNS zone to the cart.')

#Display the cart contents
cart_info = client.get(f'/order/cart/{cart_id}')
print(f'Cart contents:\n{json.dumps(cart_info, indent=4)}')

#Make sure the cart is ready to order
order = client.post(f'/order/cart/{cart_id}/checkout', autoPayWithPreferredPaymentMethod=True)
print(f'Order {order.get("orderId")} has been placed.')

order_id = cart_info['orders'][-1]

#Check the status of the order
order_status = client.get(f'/me/order/{order_id}/status')
print(f'Order {order_id} status: {order_status}')```

</details>


# 答案1
**得分**: 1

创建购物车后,在向购物车添加你的DNS区域(或任何其他产品)之前,需要*将*该购物车*分配*给你自己。

可以使用[此路径](https://api.ovh.com/console/#/order/cart/%7BcartId%7D/assign~POST)来完成:

```python
# 将购物车分配给已登录的客户
POST /order/cart/{cartId}/assign

因此,你的脚本应该如下所示:

import ovh

# 对于这3个密钥,请参考文档:
# - https://github.com/ovh/python-ovh#use-the-api-on-behalf-of-a-user
# - 通过 https://www.ovh.com/auth/api/createToken 轻松生成密钥

client = ovh.Client(
    endpoint='ovh-eu',
    application_key='<app_key>',
    application_secret='<app_secret>',
    consumer_key='<consumer_key>'
)
# 初始化一个新的购物车
cart = client.post(
    '/order/cart',
    ovhSubsidiary='PL'
)
cart_id = cart.get('cartId')

# 将此购物车分配给当前已登录的用户
assign = client.post(
    f'/order/cart/{cart_id}/assign'
)

要订购的区域 = ['domain-1.ovh', 'domain-2.ovh', 'domain-3.ovh']
for domain in zones_to_order:
    # 向购物车添加新的DNS区域
    result = client.post(
        f'/order/cart/{ cart_id }/dns',
        duration="P1Y",
        planCode="zone",
        pricingMode="default"
    )
    itemId = result.get("idemId")
    # 在购物车中配置DNS区域
    client.post(
        f'/order/cart/{cart_id}/item/{ itemId }/configuration',
        label="zone",
        value=domain
    )

# 完成您的订单
checkout = client.post(
    f'/order/cart/{cart_id}/checkout'
)
英文:

After creating the cart, and before adding your DNS zone (or any other products) to this cart, you need to assign this cart to yourself.

It can be done with this route:

# Assign a shopping cart to an loggedin client
POST /order/cart/{cartId}/assign

So your script should looks like:

import ovh

# For these 3 keys, refer to the documentation:
# - https://github.com/ovh/python-ovh#use-the-api-on-behalf-of-a-user
# - Generate the keys easily via https://www.ovh.com/auth/api/createToken

client = ovh.Client(
    endpoint=&#39;ovh-eu&#39;,
    application_key=&#39;&lt;app_key&gt;&#39;,
    application_secret=&#39;&lt;app_secret&gt;&#39;,
    consumer_key=&#39;&lt;consumer_key&gt;&#39;
)
# Initiate a new cart
cart = client.post(
    &#39;/order/cart&#39;,
    ovhSubsidiary=&#39;PL&#39;
)
cart_id = cart.get(&#39;cartId&#39;)

# Assign this cart to the currently logged-in user
assign = client.post(
    f&#39;/order/cart/{cart_id}/assign&#39;
)

zones_to_order = [&#39;domain-1.ovh&#39;, &#39;domain-2.ovh&#39;, &#39;domain-3.ovh&#39;]
for domain in zones_to_order:
    # Add a new DNS zone to the cart
    result = client.post(
        f&#39;/order/cart/{ cart_id }/dns&#39;,
        duration=&quot;P1Y&quot;,
        planCode=&quot;zone&quot;,
        pricingMode=&quot;default&quot;
    )
    itemId = result.get(&quot;idemId&quot;)
    # Configure the DNS zone in the cart
    client.post(
        f&#39;/order/cart/{cart_id}/item/{ itemId }/configuration&#39;,
        label=&quot;zone&quot;,
        value=domain
    )

# Finalyze your order
checkout = client.post(
    f&#39;/order/cart/{cart_id}/checkout&#39;
)

huangapple
  • 本文由 发表于 2023年2月16日 19:45:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定