C# 帮助理解令牌交换服务以及如何实现令牌交换服务?
我需要一些关于如何实现代币交换服务的建议和指导 我正在尝试实现这个SpotifyApi包装发现(johnnycrazy.github.io/SpotifyApi NET/auth/token#u swap.html#使用TokenSwapWebAPI工厂)所涉及的步骤[此处] 以下是链接中的主要代码段:C# 帮助理解令牌交换服务以及如何实现令牌交换服务?,c#,spotify,C#,Spotify,我需要一些关于如何实现代币交换服务的建议和指导 我正在尝试实现这个SpotifyApi包装发现(johnnycrazy.github.io/SpotifyApi NET/auth/token#u swap.html#使用TokenSwapWebAPI工厂)所涉及的步骤[此处] 以下是链接中的主要代码段: TokenSwapWebAPIFactory webApiFactory; SpotifyWebAPI spotify; // You should store a reference to
TokenSwapWebAPIFactory webApiFactory;
SpotifyWebAPI spotify;
// You should store a reference to WebAPIFactory if you are using AutoRefresh or want to manually refresh it later on. New WebAPIFactory objects cannot refresh SpotifyWebAPI object that they did not give to you.
webApiFactory = new TokenSwapWebAPIFactory("exchangeServerUri")
{
Scope = Scope.UserReadPrivate | Scope.UserReadEmail | Scope.PlaylistReadPrivate,
AutoRefresh = true
};
// You may want to react to being able to use the Spotify service.
// webApiFactory.OnAuthSuccess += (sender, e) => authorized = true;
// You may want to react to your user's access expiring.
// webApiFactory.OnAccessTokenExpired += (sender, e) => authorized = false;
try
{
spotify = await webApiFactory.GetWebApiAsync();
// Synchronous way:
// spotify = webApiFactory.GetWebApiAsync().Result;
}
catch (Exception ex)
{
// Example way to handle error reporting gracefully with your SpotifyWebAPI wrapper
// UpdateStatus($"Spotify failed to load: {ex.Message}");
}
我有几个问题:
- 如果结果返回到spotifywebapi对象,exchangeServerUri的用途是什么
- 如果我在启动时放置它(我是否在启动时放置它)。。如何访问控制器中的SpotifyWebAPI对象
exchangeServerUri
(暂时不要这样做)
同样的,下面还有更多的解释:
为了使您的客户机完全保密,并使您的客户机ID和重定向URI尽可能安全,需要使用web服务器(如php网站)
要使用此方法,外部HTTP服务器(您可能需要创建)需要能够向应用程序提供以下HTTP端点:
为了简洁起见,我跳过了一些文字
以下开源令牌交换端点代码可用于您的网站:
因此,简而言之,您需要建立一个单独的Web服务器,并将其位置提供给exchangeServerUri
然而
该文档还指出,只有在您使用授权\u code
授权类型并且担心泄露您的Spotify应用程序机密时,实施此Exchange服务才有意义(请阅读:如果您正在构建移动应用程序,用户可能会对其进行反向工程并提取您的机密)。鉴于您关于使用隐式授权的声明
意味着这不适用于您
如果有疑问,请查看Spotify自己的,看看哪种流最适合您。也检查
使用隐式授权的示例参见
第二个问题的答案在很大程度上取决于您编写的应用程序的类型,但一般来说,我会尝试将payload.TokenType
和payload.AccessToken
持久化,并在需要的地方实例化SpotifyWebAPI
。就上面的代码示例而言,您只能在特定事件处理程序的范围内使用api
对象
static async void Main(string[] args)
{
ImplicitGrantAuth auth = new ImplicitGrantAuth(
_clientId,
"http://localhost:4002",
"http://localhost:4002",
Scope.UserReadPrivate
);
auth.AuthReceived += async (sender, payload) =>
{
auth.Stop(); // `sender` is also the auth instance
SpotifyWebAPI api = new SpotifyWebAPI()
{
TokenType = payload.TokenType,
AccessToken = payload.AccessToken
};
// Do requests with API client
};
auth.Start(); // Starts an internal HTTP Server
auth.OpenBrowser();
}