【问题标题】:How do I pass my subscription key without putting it through terminal?如何在不通过终端的情况下传递我的订阅密钥?
【发布时间】:2018-03-14 05:02:41
【问题描述】:

因此,在 python sdk 上,在 CreateProfile.py 上使用 Microsoft 认知进行说话人识别,我将订阅密钥设置在变量 subscritionKey 下(注意:在此示例中设置为变量的值不是我的实际产品密钥) 但是当我将它放入函数create_profile 的参数之一时,我得到了错误...

Exception: Error creating profile: {"error":{"code":"Unspecified","message":"Access denied due to invalid subscription key. Make sure you are subscribed to an API you are trying to call and provide the right key."}}

是否可以传递我的订阅密钥而不必每次都通过终端不断输入?

import IdentificationServiceHttpClientHelper
import sys

subscritionKey = "j23h4i32h4iu3324iu234h233b43"

def create_profile(subscription_key, locale):
    """Creates a profile on the server.
    Arguments:
    subscription_key -- the subscription key string
    locale -- the locale string
    """
    helper = IdentificationServiceHttpClientHelper.IdentificationServiceHttpClientHelper(
        subscription_key)

    creation_response = helper.create_profile(locale)

    print('Profile ID = {0}'.format(creation_response.get_profile_id()))

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print('Usage: python CreateProfile.py <subscription_key>')
        print('\t<subscription_key> is the subscription key for the service')
        #sys.exit('Error: Incorrect Usage.')

    create_profile(subscritionKey, 'en-us')

我的猜测是我遇到了问题,因为我将它作为字符串传递:/

【问题讨论】:

  • 请注意,您的代码的某些地方有错字 (subscritionKey),而其他地方的拼写正确。

标签: python microsoft-cognitive azure-cognitive-services


【解决方案1】:

您的问题基本上是如何使用SDK,对吧?

以下代码适用于我:在一个名为 main.py 的文件中,该文件位于文件夹 Identification 之外的一层:

import sys
sys.path.append('./Identification')
from CreateProfile import create_profile

subscriptionKey = "<YOUR-KEY>"

create_profile(subscriptionKey, "en-us")

运行 python main.py(使用 Python 3),该代码返回

Profile ID = cf04bf79-xxxx-xxxxx-xxxx

【讨论】:

  • 您确定您使用的 API 密钥与说话人识别对应吗?在 Azure 门户中,它应该显示“API 类型:扬声器识别 API(预览版)”
  • 看到你的回答我就知道了。这让我更深入地挖掘,我发现我的问题是我的订阅密钥_SUBSCRIPTION_KEY_HEADER,这就是给我错误的原因。我会将其标记为解决方案,因为它回答了问题,但问题已解决>.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-30
  • 2013-08-14
  • 2011-06-16
  • 2020-09-06
  • 2021-12-21
  • 2019-09-06
  • 2015-11-02
相关资源
最近更新 更多