- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想利用函数应用程序进行一系列连续的 api 调用,每天将文件下载到存储帐户。我正在利用第一个函数来使用身份验证代码检索刷新 token ,然后添加到正常工作的存储表(也可以使用 key 保管库)(AuthHTTPTrigger)。从现在起,我需要将存储的刷新 token 传递到下一个 api 调用中,以接收访问 token 并更新返回到存储表中的新刷新 token 。下一个 API 调用将传入访问 token 来创建所述报告。此调用现在生成一个 reportId,需要将其与访问 token 一起传递到最后一个 api 调用 url,最终通过安全链接生成报告(本质上将使用计时器来刷新调用,直到 param 不为 null)。
由于每次调用都依赖于前一个调用来传递参数,是否有一种简单的方法可以进行顺序 api 调用?我目前的想法是利用await 任务和await all 以及一个计时器来等待文件的下载链接,为每个api 调用创建另一个函数和单独的任务。
最终,我想手动启动此功能一次,并利用刷新 token ,因为访问 token 每天都会过期,同时每天都会更新刷新 token 。
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace PartnerCenterAPI.Properties
{
public static class AuthHttpTrigger
{
[FunctionName("AuthHttpTrigger")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
ILogger log)
{
if (log is null)
{
throw new ArgumentNullException(nameof(log));
}
// Get the authentication code from the request payload
string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
string authCode = data.authCode;
// Get the Application details from the settings
string tenantId = Environment.GetEnvironmentVariable("TenantId", EnvironmentVariableTarget.Process);
string clientId = Environment.GetEnvironmentVariable("ClientId", EnvironmentVariableTarget.Process);
string clientSecret = Environment.GetEnvironmentVariable("ClientSecret", EnvironmentVariableTarget.Process);
string storageConnectionString = Environment.GetEnvironmentVariable("TableStorage", EnvironmentVariableTarget.Process);
// Get the access and refresh token from MS Identity
MicrosoftIdentityClient idClient = new(clientId, clientSecret, tenantId);
(string idToken, string accessToken, string refreshToken) = await idClient.GetAccessTokenFromAuthorizationCode(authCode);
// Save the refresh token to an Azure Storage Table
AzureStorageClient azureStorageClient = new(storageConnectionString);
await azureStorageClient.AddOrUpdateRefreshToken(clientId, MicrosoftIdentityClient.GetIdTokenUniqueUser(idToken), refreshToken);
return new OkObjectResult(new ReturnValue
{
AccessToken = accessToken,
IdToken = idToken,
RefreshToken = refreshToken,
});
}
}
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace PartnerCenterAPI.Properties
{
public class MicrosoftIdentityClient
{
private static readonly HttpClient httpClient = new();
private static readonly string hostUrl = "https://login.microsoftonline.com";
private readonly string tenantId;
private readonly string clientId;
private readonly string clientSecret;
public MicrosoftIdentityClient(string clientId, string clientSecret, string tenantId)
{
this.clientId = clientId;
this.clientSecret = clientSecret;
this.tenantId = tenantId;
}
public static string GetIdTokenUniqueUser(string idToken)
{
JwtSecurityToken securityToken = new(idToken);
string tid = securityToken.Payload.Claims.FirstOrDefault(claim => claim.Type == "tid").Value;
string sub = securityToken.Payload.Claims.FirstOrDefault(claim => claim.Type == "sub").Value;
return Convert.ToBase64String(Encoding.UTF8.GetBytes($"{tid}{sub}"));
}
public async Task<(string idToken, string accessToken, string refreshToken)> GetAccessTokenFromAuthorizationCode(string authCode)
{
string redirectUrl = "https://partnercenterxxxx.azurewebsites.net";
string scopes = "openid offline_access https://api.partnercenter.microsoft.com/user_impersonation";
Uri requestUri = new($"{hostUrl}/{tenantId}/oauth2/v2.0/token");
List<KeyValuePair<string, string>> content = new()
{
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("scope", scopes),
new KeyValuePair<string, string>("grant_type", "authorization_code"),
new KeyValuePair<string, string>("code", authCode),
new KeyValuePair<string, string>("redirect_uri", redirectUrl),
new KeyValuePair<string, string>("client_secret", clientSecret)
};
HttpRequestMessage request = new(HttpMethod.Post, requestUri)
{
Content = new FormUrlEncodedContent(content),
};
HttpResponseMessage response = await httpClient.SendAsync(request);
string responseContent = await response.Content.ReadAsStringAsync();
dynamic responseObject = JsonConvert.DeserializeObject(responseContent);
if (response.IsSuccessStatusCode)
{
// dynamic values need to be assigned before passing back
return (responseObject.id_token, responseObject.access_token, responseObject.refresh_token);
}
else if (response.StatusCode == HttpStatusCode.BadRequest)
{
// Something failed along the way, and there will be an error in there if the error code is 400
// Handle it however you want.
throw new Exception((string)responseObject.error_description);
}
else
{
// ¯\_(ツ)_/¯
throw new Exception("Something bad happened");
}
}
public async Task<(string idToken, string accessToken, string refreshToken)> GetAccessTokenFromRefreshToken(string refreshToken)
{
string scopes = "openid offline_access https://api.partnercenter.microsoft.com/user_impersonation";
Uri requestUri = new($"{hostUrl}/{tenantId}/oauth2/v2.0/token");
List<KeyValuePair<string, string>> content = new()
{
new KeyValuePair<string, string>("client_id", clientId),
new KeyValuePair<string, string>("scope", scopes),
new KeyValuePair<string, string>("grant_type", "refresh_token"),
new KeyValuePair<string, string>("refresh_token", refreshToken),
new KeyValuePair<string, string>("client_secret", clientSecret)
};
HttpRequestMessage request = new(HttpMethod.Post, requestUri)
{
Content = new FormUrlEncodedContent(content),
};
HttpResponseMessage response = await httpClient.SendAsync(request);
string responseContent = await response.Content.ReadAsStringAsync();
dynamic responseObject = JsonConvert.DeserializeObject(responseContent);
if (response.IsSuccessStatusCode)
{
return (responseObject.id_token, responseObject.access_token, responseObject.refresh_token);
}
else if (response.StatusCode == HttpStatusCode.BadRequest)
{
// Something failed along the way, and there will be an error in there if the error code is 400
// Handle it however you want.
throw new Exception((string)responseObject.error_message);
}
else
{
// ¯\_(ツ)_/¯
throw new Exception("Something bad happened");
}
}
}
}
using Microsoft.Azure.Cosmos.Table;
using System.Threading.Tasks;
namespace PartnerCenterAPI.Properties
{
public class AzureStorageClient
{
private readonly CloudTable refreshTokenTable;
public AzureStorageClient(string connectionString)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudTableClient tableClient = storageAccount.CreateCloudTableClient(new TableClientConfiguration());
refreshTokenTable = tableClient.GetTableReference("refreshTokens");
refreshTokenTable.CreateIfNotExists();
}
public async Task<RefreshTokenEntity> AddOrUpdateRefreshToken(string audience, string userId, string refreshToken)
{
RefreshTokenEntity tokenEntity = new(audience, userId)
{
RefreshToken = refreshToken
};
TableResult tableResult = await refreshTokenTable.ExecuteAsync(TableOperation.InsertOrReplace(tokenEntity));
return tableResult.Result as RefreshTokenEntity;
}
}
}
using Microsoft.Azure.Cosmos.Table;
using System;
namespace PartnerCenterAPI.Properties
{
public class RefreshTokenEntity : TableEntity
{
public string RefreshToken { get; set; }
public RefreshTokenEntity(string audience, string userId)
{
RefreshTokenEntity refreshTokenEntity = this;
refreshTokenEntity.PartitionKey = audience;
refreshTokenEntity.RowKey = userId;
}
public static implicit operator string(RefreshTokenEntity v)
{
throw new NotImplementedException();
}
}
}
最佳答案
Create another function and separate tasks for each api call utilizing await task and await all and a timer to await the download link of the file are my current thoughts.
当然,你可以做到。
您还可以使用durable functions .
关于c# - 按顺序调用多个 API 来检索和传递 token C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73111489/
如果我使用下面的代码,数据将为零 dispatch_async(dispatch_get_global_queue(0,0), ^{ UIImage *img = [[UIImage allo
fread来自 data.table包一般可以在读取文件时自动确定列分隔符( sep )。 例如,这里fread自动检测 |作为列分隔符: library(data.table) fread(past
因此,如果我有一个如下所示的数据框: A B C rowname1 4.5 4 3.2 rowname2 3 23
我有一个汽车模型的搜索数据库:“日产Gtr”,“Huynday Elantra”,“Honda Accord”等。 现在我还有一个用户列表和他们喜欢的汽车类型 user1喜欢:carId:1234,c
我正在使用 Javamail 来获取一些电子邮件数据。我将用户输入作为电子邮件 ID、imap 地址和密码并连接到 imap。然后我监视收件箱的电子邮件并查明此人是否在“收件人”或“抄送”中。 Ema
我有一些数据,我想根据差距统计来评估最佳簇数。 我阅读了 gap statistic 上的页面在 r 中给出了以下示例: gs.pam.RU Number of clusters (method '
我有一个用户名和密码组合,我将使用它通过 java 代码访问安全服务器。 我的想法是: 在外部存储加密凭据 执行时提示用户输入解密密码 在使用前将解密的凭据直接存储在字符数组中 使用凭据连接到数据库
这是 Firebase 数据:[Firebase 数据][1] 我必须从员工那里检索所有字段并将其存储在一个数组中。 现在数据更改 toast 消息即将到来,但已经很晚了。 Firebase.setA
我是 iOS 的新手,正在开发一个基本的应用程序,它目前正在使用 SSKeychain 和 AFNetworking 与 API 进行交互。当您使用我检索的应用程序登录并在我的 CredentialS
编辑:这个问题已经在 apphacker 和 ConcernedOfTunbridgeWells 的帮助下得到解决。我已更新代码以反射(reflect)我将使用的解决方案。 我目前正在编写一个群体智能
我是 C 的新手,我想编写一个程序来检查用户输入的单词是否合法。我已经在 stackoverflow 上搜索了建议,但很多都是针对特定情况的。请在我被激怒之前,我知道这个语法不正确,但正在寻找一些关于
我相信你们中的一些人编写过 C# 类,这些类必须从数据库设置密码/从数据库获取密码。 我假设敏感细节不会以明文形式显示。处理此类数据的推荐程序是什么?检索到的文本是否加密?您是否将 pws 存储在加密
我在 linux 上使用 2.7 之前的 python 版本,想知道如何检索 RUID? 2.7 及更高版本从 os 包中获得了 getresuid,但我似乎找不到 2.6 的等效项 最佳答案 您可以
我已经在 Android 中实现了一个存储对象的标准 LRUCache。每个键都是与存储的对象关联的唯一 ObjectId。我的问题是从缓存中检索对象的唯一方法是通过 ObjectId(无迭代器)。实
这已经被问过很多次了。解决方案(对我有用)是从 packages.config 文件(这就足够了)和 packages 文件夹中删除 *** 包。 这对我来说是一个糟糕的解决方案,因为每次我想安装一些
我有以下文字: #{king} for a ##{day}, ##{fool} for a #{lifetime} 以及以下(损坏的)正则表达式: [^#]#{[a-z]+} 我想匹配所有#{word
我正在寻找一种快速(如高性能,而不是快速修复)解决方案来持久化和检索数千万个小型(大约 1k)二进制对象。每个对象都应该有一个用于检索的唯一 ID(最好是 GUID 或 SHA)。额外的要求是它应该可
有没有办法获取 RegInit 的重置值?通过探测产生的类型的成员?我可以看到 RegInit 将返回类型(例如 UInt )。例如,我将有一个寄存器,我想通过 regmap 对其进行控制。 val
Iv 目前接手了一个项目,其中开发人员在某些表的 json 数组列中存储了 has many 关系。 产品表 ---------------------------- id | product | c
Git 会在任何地方记录推送到远程的历史吗? 我注意到我们能够在 Microsoft VSTS 中查看 Git 存储库的推送历史记录以及每次推送的相关提交。它甚至显示旧的、过时的提交,由于后来的强制推
我是一名优秀的程序员,十分优秀!