arcgis pro 如何在应用程序外授权 Python
时间: 2024-05-20 07:14:25 浏览: 170
arcgis pro for python.rar
5星 · 资源好评率100%
如果您想在ArcGIS Pro应用程序外授权Python,则需要执行以下步骤:
1. 获得ArcGIS Pro的许可证文件 (.ecp)
2. 从 ArcGIS for Developers 网站下载 ArcGISProSDK.exe。
3. 运行 ArcGISProSDK.exe 安装 ArcGIS Pro SDK。
4. 打开 Visual Studio 并创建一个新的 C# 控制台应用程序。
5. 右键单击项目并选择“管理 NuGet 程序包”。
6. 搜索“Esri.ArcGISRuntime.WPF”并安装该程序包。
7. 将以下代码添加到 Main 方法中:
```
using System;
using System.Threading.Tasks;
using Esri.ArcGISRuntime;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Security;
using Esri.ArcGISRuntime.UI.Controls;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// Set the client ID for authorization.
string clientId = "your_client_id_here";
// Call a function to authorize the app with the provided client ID.
Task.Run(() => AuthorizeWithPortalAsync(clientId)).Wait();
// Create a map with a basemap.
Map myMap = new Map(Basemap.CreateTopographic());
// Create a new map view control.
MapView myMapView = new MapView();
// Assign the map to the map view.
myMapView.Map = myMap;
// Zoom to a specific location.
Envelope initialLocation = new Envelope(-118.6919, 34.0205, -118.1909, 34.3114, SpatialReferences.Wgs84);
myMapView.SetViewpoint(new Viewpoint(initialLocation));
// Wait for input before exiting.
Console.WriteLine("Press ENTER to exit.");
Console.ReadLine();
}
private static async Task AuthorizeWithPortalAsync(string clientId)
{
// Register the server information with the AuthenticationManager.
ServerInfo serverInfo = new ServerInfo
{
ServerUri = new Uri("https://www.arcgis.com/sharing/rest"),
TokenAuthenticationType = TokenAuthenticationType.OAuthAuthorizationCode,
OAuthClientInfo = new OAuthClientInfo
{
ClientId = clientId,
RedirectUri = new Uri("urn:ietf:wg:oauth:2.0:oob")
}
};
AuthenticationManager.Current.RegisterServer(serverInfo);
// Generate a new authorization code.
string authorizationCode = await GenerateAuthorizationCodeAsync();
// Get a token using the authorization code.
await AuthenticationManager.Current.GenerateTokenAsync(serverInfo, authorizationCode);
}
private static async Task<string> GenerateAuthorizationCodeAsync()
{
// Create a new instance of the authentication view model.
AuthenticationViewModel authViewModel = new AuthenticationViewModel();
// Show the authentication view.
bool success = await AuthenticationManager.Current.ShowOAuthAuthorizeDialogAsync(authViewModel);
// If the user authorized the app, return the authorization code.
if (success)
{
return authViewModel.AuthorizationCode;
}
// If the user didn't authorize the app, throw an exception.
throw new Exception("Authorization failed.");
}
}
}
```
8. 替换代码中的“your_client_id_here”为您的 ArcGIS 开发者帐户的客户端 ID。
9. 运行应用程序并按照屏幕上的提示进行操作以完成授权。
完成以上步骤后,您的应用程序就可以在授权之后使用 ArcGIS Pro 的 Python 功能了。
阅读全文