【发布时间】:2021-09-03 14:43:04
【问题描述】:
我正在为自己编写一个非官方的 twitter api。然后我通过以下方法使用浏览器中的控制台屏幕向此 api 发送一个 get。
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
}
这样的
httpGet(https://localhost:44311/WeatherForecast/alienationxs/true);
问题是,当我通过www.google.com 执行此操作时,一切正常,并且 json 数据到达了我的手中。但是当我通过 twitter.com 执行此操作时,我收到以下错误。
通过 google.com
我在 api 上的 cors 设置
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors(options =>
options.AddDefaultPolicy(builder =>
builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin())); ;
services.AddMvc();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "twitterAPI", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "twitterAPI v1"));
}
app.UseRouting();
app.UseCors(builder => builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
我只想通过 twitter.com 访问我的 api,就像 google.com 一样。
【问题讨论】:
-
如果你能连接google.com,你应该先确保这个twitter api可以访问
标签: asp.net-web-api asp.net-core-webapi content-security-policy