gpt4 book ai didi

使用 EnvironmentCredential 几乎不可能进行 Azure 量子身份验证

转载 作者:行者123 更新时间:2023-12-03 06:54:08 25 4
gpt4 key购买 nike

下面用于解决 Azure 量子上的简单问题的代码永远不会起作用。 Microsoft,请帮助我进行身份验证。

from azure.quantum.optimization import Problem, ProblemType, Term
from typing import List
from azure.quantum.optimization import Term
from azure.quantum import Workspace
from azure.identity import EnvironmentCredential
from azure.quantum.optimization import ParallelTempering

problem = Problem(name="My First Problem", problem_type=ProblemType.ising)

workspace = Workspace (
subscription_id = "my-subscription-id", # Add your subscription_id
resource_group = "AzureQuantum", # Add your resource_group
name = "my-workspace-name", # Add your workspace name
location = "my-workspace-location" , # Add your workspace location (for example, "westus")
credential = EnvironmentCredential(AZURE_USERNAME="my-email-id", AZURE_PASSWORD="my-microsoft-password")
# credential = ManagedIdentityCredential()
)

terms = [
Term(c=-9, indices=[0]),
Term(c=-3, indices=[1,0]),
Term(c=5, indices=[2,0]),
Term(c=9, indices=[2,1]),
Term(c=2, indices=[3,0]),
Term(c=-4, indices=[3,1]),
Term(c=4, indices=[3,2])
]

problem.add_terms(terms=terms)



solver = ParallelTempering(workspace, timeout=100)

result = solver.optimize(problem)
print(result)

上面的代码抛出错误:

EnvironmentCredential.get_token failed: EnvironmentCredential authentication unavailable. Environment variables are not fully configured.
Visit https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot to troubleshoot.this issue.
---------------------------------------------------------------------------
CredentialUnavailableError Traceback (most recent call last)
[<ipython-input-19-90cd448f8194>](https://localhost:8080/#) in <module>()
3 solver = ParallelTempering(workspace, timeout=100)
4
----> 5 result = solver.optimize(problem)
6 print(result)

19 frames
[/usr/local/lib/python3.7/dist-packages/azure/identity/_credentials/environment.py](https://localhost:8080/#) in get_token(self, *scopes, **kwargs)
141 "this issue."
142 )
--> 143 raise CredentialUnavailableError(message=message)
144 return self._credential.get_token(*scopes, **kwargs)

CredentialUnavailableError: EnvironmentCredential authentication unavailable. Environment variables are not fully configured.
Visit https://aka.ms/azsdk/python/identity/environmentcredential/troubleshoot to troubleshoot this issue.

详细信息

当我不向工作区传递任何凭据时,上面的代码工作得很好,但这会弹出一个身份验证窗口。 我不想每次运行某些内容进行身份验证时都手动单击浏览器。我只想轻松地在代码中传递凭据无需处理文档中为身份验证定义的所有复杂事物。

注意:我在 EnvironmentCredential 中传递我的电子邮件和密码(我显然没有在这里编写 secret 信息,例如在工作区中传递的订阅 ID)

最佳答案

EnvironmentCredential 从环境变量中获取参数,而不是从构造函数中的参数获取参数,因此为了避免提示输入凭据,您需要使用正确的值设置相应的环境变量,然后运行你的程序。像这样的东西:

set AZURE_USERNAME="my-email-id"
set AZURE_PASSWORD="my-microsoft-password"
python myprogram.py

在您的代码中,您将执行以下操作:

workspace = Workspace (
subscription_id = "my-subscription-id", # Add your subscription_id
resource_group = "AzureQuantum", # Add your resource_group
name = "my-workspace-name", # Add your workspace name
location = "my-workspace-location" , # Add your workspace location (for example, "westus")
credential = EnvironmentCredential()
)

话虽这么说,不提示输入凭据的更简单方法是安装 Azure CLI并使用 az login 登录;这将提示一次输入凭据,然后将它们保留在您的计算机本地,这样您就不必再次登录。

如果您使用 VS Code,则可以选择安装 Azure Account扩展名。

对于所有这些选项,您无需在 AzureQuantum 构造函数中提供任何凭据。它将自动尝试发现您是否已设置环境变量、使用 CLI 或扩展来登录并使用它。仅当找不到其他内容时,它才会默认在浏览器上进行提示。

关于使用 EnvironmentCredential 几乎不可能进行 Azure 量子身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73278480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com