api 相关问题

请勿使用:使用您所指的库,[api-design]或其他适当的标签进行标记。要求我们推荐或查找API的问题是偏离主题的。

Python API 调用 Random.org

所以我对 API 很陌生,但我正在尝试更多地练习它们。我的连接正常(200),但每当我尝试打印结果时,我都会收到此错误: {“jsonrpc”:“2.0”,“错误”:{“代码”:-

回答 3 投票 0

当我从 fetch 调用中调用数据时,一个部分会抛出错误,但仍然正确登录控制台,而其他所有部分都工作正常

我正在使用 DND5E API 并做出反应。我调用数据并将其保存到一个名为 PrimaryChoices 的变量中,该变量是一个从类部分下的 api 中的 `proficiency_choices 开始的数组 (https://www.

回答 1 投票 0

如何将文件编码为 Synology vsmeta?

我是这个网站的新手,所以如果我忘记了这个问题的任何重要信息,请随时告诉我。 我在 Synology NAS 上使用 VideoStation,到目前为止,它提供了获得...

回答 1 投票 0

使用私有 API 进行 authController 和用户模型

我的 Laravel 站点中有一个现有的 authcontroller 和用户模型,它已经工作了很长时间,但我现在需要对其进行修改,以便不必显式地为用户访问数据库......

回答 1 投票 0

使用rest api创建post wordpress.com

我想制作 php 应用程序使用 REST api 在 wordpress.com 上创建帖子。 我使用这段代码: 我想制作 php 应用程序使用 REST api 在 wordpress.com 上创建帖子。 我使用这个代码: <?php $curl = curl_init( 'https://public-api.wordpress.com/oauth2/token' ); curl_setopt( $curl, CURLOPT_POST, true ); curl_setopt( $curl, CURLOPT_POSTFIELDS, array( 'client_id' => 12345, 'redirect_uri' => 'http://example.com/wp/test.php', 'client_secret' => 'L8RvNFqyzvqh25P726jl0XxSLGBOlVWDaxxxxxcxxxxxxx', 'code' => $_GET['code'], // The code fromthe previous request 'grant_type' => 'authorization_code' ) ); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1); $auth = curl_exec( $curl ); $secret = json_decode($auth); $access_token = $secret->access_token; $post = array( 'title'=>'Hello World', 'content'=>'Hello. I am a test post. I was created by the API', 'date'=>date('YmdHis'), 'categories'=>'API','tags=tests' ); $post = http_build_query($post); $apicall = "https://public-api.wordpress.com/rest/v1/sites/mysite.wordpress.com/posts/new"; $ch = curl_init($apicall); curl_setopt($ch, CURLOPT_HTTPHEADER, array ('authorization: Bearer ' . $access_token,"Content-Type: application/x-www-form-urlencoded; charset=utf-8")); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_VERBOSE, 1); $return = curl_exec($ch); echo "<pre>"; print_r($return); exit; ?> 但我收到此错误: {"error":"未经授权","message":"用户无法发布帖子"} 可以帮助我吗? 谢谢 创建帖子的标准方法是使用 cookies 和随机数。 但是我找到了一种更简单的方法。 将 Basic-Auth 插件安装到您的 WordPress。 使用用户名admin和密码admin创建wordpress用户(这两个凭据都是不安全,仅用于演示目的) 使用代码创建帖子: $username = 'admin'; $password = 'admin'; $rest_api_url = "http://my-wordpress-site.com/wp-json/wp/v2/posts"; $data_string = json_encode([ 'title' => 'My title', 'content' => 'My content', 'status' => 'publish', ]); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $rest_api_url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string), 'Authorization: Basic ' . base64_encode($username . ':' . $password), ]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec($ch); curl_close($ch); if ($result) { // ... } else { // ... } 请注意,上面的示例中使用了 REST API 版本 2。 答案是对的,我们可以使用“Basic-Auth”插件来发出 Rest API 请求。 但是,@vallez 想在 wordpress.com 网站上创建帖子。 wordpress.com 提供了 oAuth 支持进行身份验证。 最近我创建了一篇帖子,演示如何使用 oAuth 在 wordpress.com 上创建帖子。您可以阅读该文章:使用 oAuth 和 Rest API 在 wordpress.com 网站上创建帖子 以下是在 wordpress.com 上使用 oAuth 成功创建帖子的步骤。 第 1 步: 添加身份验证详细信息以获取身份验证密钥。 $auth_args = array( 'username' => '', 'password' => '', 'client_id' => '', 'client_secret' => '', 'grant_type' => 'password', // Keep this as it is. ); $access_key = get_access_key( $auth_args ); 下面是函数 get_access_key() 返回访问密钥。 第 2 步: 获取访问密钥。 /** * Get Access Key. * * @param array $args Auth arguments. * @return mixed Auth response. */ function get_access_key( $args ) { // Access Token. $curl = curl_init( 'https://public-api.wordpress.com/oauth2/token' ); curl_setopt( $curl, CURLOPT_POST, true ); curl_setopt( $curl, CURLOPT_POSTFIELDS, $args ); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1); $auth = curl_exec( $curl ); $auth = json_decode($auth); // Access Key. return $auth->access_token; } 第 3 步: 设置帖子参数并传递它来创建帖子。 $post_args = array( 'title' => 'Test Post with oAuth', 'content' => 'Test post content goes here..', 'tags' => 'tests', 'post_status' => 'draft', 'categories' => 'API', ); 第 4 步: 使用访问密钥创建帖子。 现在,我们有了访问密钥和创建帖子参数。所以,让我们将它们传递给函数 create_post()。 create_post( $access_key, $post_args ); 第 5 步: 使用访问密钥创建帖子。 /** * Create post with access key. * * @param string $access_key Access key. * @param array $post_args Post arguments. * @return mixed Post response. */ function create_post( $access_key, $post_args ) { $options = array ( 'http' => array( 'ignore_errors' => true, 'method' => 'POST', 'header' => array( 0 => 'authorization: Bearer ' . $access_key, 1 => 'Content-Type: application/x-www-form-urlencoded', ), 'content' => http_build_query( $post_args ), ), ); $context = stream_context_create( $options ); $response = file_get_contents( 'https://public-api.wordpress.com/rest/v1/sites/YOURSITEID/posts/new/', false, $context ); return json_decode( $response ); } 如果有人在使用带有应用程序密码的 REST API 时收到 401 未经授权的错误,请确保您使用 WordPress 用户名(而不是应用程序密码名称) 和应用程序密码 {"code":"rest_cannot_manage_templates","message":"Sorry, you are not allowed to access the templates on this site.","data":{"status":401}}

回答 3 投票 0

使用 API 请求在盈透证券下订单

首先,我成功地使用TWS API下订单。然而,据我了解,为此,我需要在后台运行 TWS 桌面版本。但我需要运行这个......

回答 2 投票 0

测试我的路线时,我收到错误 404 在 Postman 上找不到

这是我的类别表迁移文件中包含的代码: 这是我的类别表迁移文件中包含的代码: <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('categories', function (Blueprint $table) { $table->id(); $table->foreignId('parent_id')->nullable()->constrained('categories')->onDelete('cascade'); $table->string('name'); $table->string('icon')->nullable(); $table->timestamps(); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('categories'); } }; 这是我的 Category.php 模型文件中包含的代码: <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Category extends Model { use HasFactory; protected $fillable = ['parent_id', 'name', 'icon']; public function scopeParentCategories($query) { return $query->whereNull('parent_id'); } public static function tree() { $allCategories = Category::get(); $rootCategories = $allCategories->whereNull('parent_id'); self::buildTree($rootCategories, $allCategories); return $rootCategories; } public function children() { return $this->hasMany(Category::class, 'parent_id'); } public function items() { return $this->belongsToMany(Item::class, 'category_items'); } public function saveSubCategories($subCategories) { foreach($subCategories as $subCategory) { $this->saveSubCategory($subCategory); } } public function saveSubCategory($subCategory) { $newSubCategory = new self(); $newSubCategory->fill($subCategory); $newSubCategory->setAttribute('parent_id', $this->id); $newSubCategory->save(); } private static function formatTree($categories, $allCategories) { foreach ($categories as $category) { $category->children = $allCategories->where('parent_id', $category->id)->values(); if ($category->children->isNotEmpty()) { self::formatTree($category->children, $allCategories); } } } } 这是我的 CategoryController.php 控制器文件中包含的代码: <?php namespace App\Http\Controllers; use App\Models\Category; use Illuminate\Http\Request; use Illuminate\Support\Facades\Validator; class CategoryController extends Controller { /** * Display a listing of the resource. */ public function index() { return Category::all(); } /** * Show the form for creating a new resource. */ public function create() { // } /** * Store a newly created resource in storage. */ public function store(Request $request) { $validator = Validator::make($request->all(), [ 'name' => 'required|string|max:255', 'icon' => 'nullable|string|max:255', 'children.*.name' => 'required|string|max:255', 'children.*.icon' => 'required|string|max:255', 'children.*.parent_id' => 'nullable|exists:categories,id', ]); if ($validator->fails()) { return response()->json(['errors' => $validator->errors()], 400); } $category = Category::create($request->only('name', 'icon')); if ($request->has('children')) { foreach ($request->children as $childData) { $child = new Category($childData); $child->parent_id = $category->id; $child->save(); } } return $category; } /** * Display the specified resource. */ public function show(Category $category) { return Category::find($category); } /** * Show the form for editing the specified resource. */ public function edit(Category $category) { // } /** * Update the specified resource in storage. */ public function update(Request $request, Category $category) { $category = Category::findOrFail($category); $validator = Validator::make($request->all(), [ 'name' => 'required|string|max:255', 'parent_id' => 'nullable|exists:categories,id', 'icon' => 'nullable|string|max:255', ]); if ($validator->fails()) { return response()->json(['errors' => $validator->errors()], 400); } $category->update($request->all()); return response()->json($category); } /** * Remove the specified resource from storage. */ public function destroy(Category $category) { $category = Category::findOrFail($category); $category->delete(); return response()->json($category); } } 这是我的 api.php 路由文件中包含的代码: <?php /* |-------------------------------------------------------------------------- | API Routes |-------------------------------------------------------------------------- | | Here is where you can register API routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | is assigned the "api" middleware group. Enjoy building your API! | */ use Illuminate\Support\Facades\Route; use App\Http\Controllers\CategoryController; use App\Http\Controllers\ItemController; use App\Http\Controllers\ItemOptionController; use App\Http\Controllers\OptionController; use App\Http\Controllers\OptionValueController; use App\Http\Controllers\OrderController; use App\Http\Controllers\OrderItemController; use App\Http\Controllers\PaymentController; use App\Http\Controllers\QRCodeController; use App\Http\Controllers\TableLayoutController; // API version prefix Route::prefix('v1')->group(function () { // Routes for managing categories Route::get('categories', [CategoryController::class, 'index']); Route::post('categories', [CategoryController::class, 'store']); Route::get('categories/{category}', [CategoryController::class, 'show']); Route::put('categories/{category}', [CategoryController::class, 'update']); Route::delete('categories/{category}', [CategoryController::class, 'destroy']); // Routes for managing items Route::get('items', [ItemController::class, 'index']); Route::post('items', [ItemController::class, 'store']); Route::get('items/{item}', [ItemController::class, 'show']); Route::put('items/{item}', [ItemController::class, 'update']); Route::delete('items/{item}', [ItemController::class, 'destroy']); // Routes for managing orders Route::get('orders', [OrderController::class, 'index']); Route::post('orders', [OrderController::class, 'store']); Route::get('orders/{order}', [OrderController::class, 'show']); Route::put('orders/{order}', [OrderController::class, 'update']); Route::delete('orders/{order}', [OrderController::class, 'destroy']); // Routes for managing payments Route::get('payments', [PaymentController::class, 'index']); Route::post('payments', [PaymentController::class, 'store']); Route::get('payments/{payment}', [PaymentController::class, 'show']); Route::put('payments/{payment}', [PaymentController::class, 'update']); Route::delete('payments/{payment}', [PaymentController::class, 'destroy']); // Routes for managing options Route::get('options', [OptionController::class, 'index']); Route::post('options', [OptionController::class, 'store']); Route::get('options/{option}', [OptionController::class, 'show']); Route::put('options/{option}', [OptionController::class, 'update']); Route::delete('options/{option}', [OptionController::class, 'destroy']); // Routes for managing option values Route::get('option-values', [OptionValueController::class, 'index']); Route::post('option-values', [OptionValueController::class, 'store']); Route::get('option-values/{optionvalues}', [OptionValueController::class, 'show']); Route::put('option-values/{optionvalues}', [OptionValueController::class, 'update']); Route::delete('option-values/{optionvalues}', [OptionValueController::class, 'destroy']); // Routes for managing item options Route::get('item-options', [ItemOptionController::class, 'index']); Route::post('item-options', [ItemOptionController::class, 'store']); Route::get('item-options/{itemoptions}', [ItemOptionController::class, 'show']); Route::put('item-options/{itemoptions}', [ItemOptionController::class, 'update']); Route::delete('item-options/{itemoptions}', [ItemOptionController::class, 'destroy']); // Routes for managing order items Route::get('order-items', [OrderItemController::class, 'index']); Route::post('order-items', [OrderItemController::class, 'store']); Route::get('order-items/{orderItem}', [OrderItemController::class, 'show']); Route::put('order-items/{orderItem}', [OrderItemController::class, 'update']); Route::delete('order-items/{orderItem}', [OrderItemController::class, 'destroy']); // Routes for managing QR codes Route::get('qr-codes', [QRCodeController::class, 'index']); Route::post('qr-codes', [QRCodeController::class, 'store']); Route::get('qr-codes/{qrCode}', [QRCodeController::class, 'show']); Route::put('qr-codes/{qrCode}', [QRCodeController::class, 'update']); Route::delete('qr-codes/{qrCode}', [QRCodeController::class, 'destroy']); // Routes for managing table layouts Route::get('table-layouts', [TableLayoutController::class, 'index']); Route::post('table-layouts', [TableLayoutController::class, 'store']); Route::get('table-layouts/{tableLayout}', [TableLayoutController::class, 'show']); Route::put('table-layouts/{tableLayout}', [TableLayoutController::class, 'update']); Route::delete('table-layouts/{tableLayout}', [TableLayoutController::class, 'destroy']); }); 我正在使用 Postman 来测试我的 Laravel 应用程序后端。我正在 docker 上运行我的数据库。所有路线均无效,但我在此处显示类别作为参考。我尝试运行 post 方法但失败了 愚蠢的错误: 必须在 bootstrap->cache->app.php 中提及基本路径 <?php use Illuminate\Foundation\Application; use Illuminate\Foundation\Configuration\Exceptions; use Illuminate\Foundation\Configuration\Middleware; return Application::configure(basePath: dirname(__DIR__)) ->withRouting( web: __DIR__.'/../routes/web.php', api: __DIR__.'/../routes/api.php', commands: __DIR__.'/../routes/console.php', health: '/up', ) ->withMiddleware(function (Middleware $middleware) { // }) ->withExceptions(function (Exceptions $exceptions) { // })->create();

回答 1 投票 0

在 python 中使用新行将变量添加到 API 有效负载

我对Python还很陌生,我想要实现的目标是在Python中向下面的有效负载添加变量;它适用于 Zeptomail API,我正在尝试自定义并将用户特定的数据添加到...

回答 1 投票 0

我一直在尝试将文件上传到 api,但收到此错误“对象作为 React 子项无效,请改用数组”

我一直在尝试向我的 api 提交文件,但我收到此 Objects are not valid as a React child 。如果您打算渲染子集合,请改用数组。下面是我的代码...

回答 1 投票 0

如何在Python 0AuthLib OLX Partner API V2中获取令牌

我研究API OLX https://developer.olx.ua/api/doc。我正在尝试在 Python 中实现授权并获取令牌。卡在接收授权码的那一刻。我是初学者,我有

回答 3 投票 0

在react-native中轻松集成

我正在尝试将 Easypaisa 付款集成到我的应用程序中。我有一个商家帐户。 这是我的代码 const requestBody = 'storeId=xxxx&amount=xx&postBackURL=xxx&orderRefNum=xx'; 常量

回答 2 投票 0

我需要创建代理服务器来防止 CORS 错误吗?

我正在构建一个简单的 React 应用程序,想要显示指定城市的 TripAdvisor 照片。 TripAdvisor 有一个 API 可以执行此操作,当我通过 Postman 发出请求时,没有问题。我得到数据了

回答 1 投票 0

Azure Databricks SQL 执行 API

我尝试通过服务主体而不是个人访问令牌创建 Databricks 令牌。下面是我运行的代码: 主机 = '主机' client_id = '客户ID' oauth_secret = '秘密密钥' 终点...

回答 1 投票 0

Oracle Clob 到 C# 字符串

我正在使用 Oracle.ManagedDataAccess.Client 将数据从 Oracle 获取到 C# 中。我正在检索的返回值是一个 Clob: cmd.Parameters.Add("return_value", OracleDbType.Clob).方向...

回答 3 投票 0

在头文件中实现的结构是否需要 __attribute__((visibility("default"))) ? (C++)

我不确定在头文件中完全实现的结构是否会在用作 sha 的一部分时对其 __attribute__((visibility("default"))) 的需求产生任何影响...

回答 2 投票 0

如何在 laravel api 中显示验证错误,同时将表单请求文件与控制器分开

我有一个表单请求文件,与我的控制器分开处理我的验证。在控制器内调用 api 后如何返回验证错误? //我的控制器 /** * 显示列表...

回答 7 投票 0

API 工件中技术依赖性的最佳实践

用例:我目前正在从事一个由多个微服务组成的项目。为了易于使用并且为了正确地对 API 进行版本控制,每个服务都提供了一个单独的 Maven 工件......

回答 1 投票 0

通过FastAPI传递对象参数

我对 FastAPI 有疑问,因为我无法理解一些东西,并且文档对我来说不清楚。 我有示例程序: 导入 json 导入argparse def args(): 解析器=argparse。

回答 1 投票 0

通过 FAST API 传递对象参数

我对 FAST API 有疑问,因为我无法理解一些内容,并且文档对我来说不清楚。 我有示例程序: 导入 json 导入argparse def args(): 解析器=argparse。

回答 1 投票 0

使用 Astro 和 React 的动态 API

我最近开始了我的网络开发之旅,并决定学习一些 javascript 来处理 API,以便学习如何从公共 API 执行获取(对于初学者来说)。我第一次尝试这个...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.