تنظيم صفحاتك في مجموعات يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يمكن أن تتفاعل "برمجة تطبيقات Google" مع واجهات برمجة التطبيقات من جميع أنحاء الويب. يوضّح هذا الدليل كيفية استخدام أنواع مختلفة من واجهات برمجة التطبيقات في النصوص البرمجية.
الاتصال بواجهات برمجة التطبيقات العامة
يمكنك استخدام خدمة UrlFetch لتقديم طلبات إلى واجهة برمجة التطبيقات مباشرةً.
يستخدم المثال التالي GitHub API للبحث عن مستودعات تحتوي على 100 نجمة أو أكثر وتتضمّن الإشارة إلى "Apps Script". لا يتطلّب طلب البيانات من واجهة برمجة التطبيقات هذا الحصول على إذن أو مفتاح واجهة برمجة تطبيقات.
تتطلّب واجهات برمجة التطبيقات التي تعمل بالنيابة عن المستخدم عادةً الحصول على إذن، وغالبًا ما يتم ذلك باستخدام بروتوكول OAuth. لا توفّر "برمجة تطبيقات Google" دعمًا مدمجًا للبروتوكول، ولكن تتوفّر مكتبات مفتوحة المصدر يمكنك استخدامها لتنفيذ عملية OAuth وإرسال بيانات الاعتماد مع طلباتك:
إنّ التعامل مع عناصر JSON يشبه التعامل مع XML، إلا أنّ تحليل عنصر JSON أو ترميزه أسهل بكثير.
إذا كانت واجهة برمجة التطبيقات المطلوبة تعرض استجابة JSON أولية لطلب ما، يمكن الوصول إلى استجابة سلسلة JSON باستخدام الطريقة HTTPResponse.getContentText(). بعد استرداد هذه السلسلة، ما عليك سوى استدعاء JSON.parse() على السلسلة للحصول على تمثيل كائن أصلي.
وبالمثل، لإنشاء تمثيل سلسلة نصية لكائن JavaScript من أجل تقديم طلب، استخدِم JSON.stringify().
vardata={'entry':{'group':{'title':'Dog Skateboarding','description':'My dog gets some serious air'},'keywords':'dog, skateboard'}}varpayload=JSON.stringify(data);//MakerequesttoAPIwithpayloadafterthispoint.
تحليل XML
إذا كانت واجهة برمجة تطبيقات خارجية تعرض استجابة XML أولية لطلب، يمكنك الوصول إلى استجابة XML باستخدام الطريقة HTTPResponse.getContentText().
عند إرسال طلبات XML إلى واجهة برمجة تطبيقات، أنشئ XML لإرساله باستخدام طرق XmlService.
varroot=XmlService.createElement('entry').setAttribute('keywords','dog, skateboard');vargroup=XmlService.createElement('group').setAttribute('title','Dog Skateboarding');.setAttribute('description','My dog gets some serious air');root.addContent(group);vardocument=XmlService.createDocument(root);varpayload=XmlService.getPrettyFormat().format(document);//MakerequesttoAPIwithpayloadafterthispoint.
تاريخ التعديل الأخير: 2025-08-04 (حسب التوقيت العالمي المتفَّق عليه)
[[["يسهُل فهم المحتوى.","easyToUnderstand","thumb-up"],["ساعَدني المحتوى في حلّ مشكلتي.","solvedMyProblem","thumb-up"],["غير ذلك","otherUp","thumb-up"]],[["لا يحتوي على المعلومات التي أحتاج إليها.","missingTheInformationINeed","thumb-down"],["الخطوات معقدة للغاية / كثيرة جدًا.","tooComplicatedTooManySteps","thumb-down"],["المحتوى قديم.","outOfDate","thumb-down"],["ثمة مشكلة في الترجمة.","translationIssue","thumb-down"],["مشكلة في العيّنات / التعليمات البرمجية","samplesCodeIssue","thumb-down"],["غير ذلك","otherDown","thumb-down"]],["تاريخ التعديل الأخير: 2025-08-04 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003eGoogle Apps Script enables interaction with various web APIs using the \u003ccode\u003eUrlFetch\u003c/code\u003e service for direct API requests.\u003c/p\u003e\n"],["\u003cp\u003eFor user-centric APIs requiring authorization, utilize open-source libraries like OAuth1 or OAuth2 for Apps Script to manage the OAuth flow.\u003c/p\u003e\n"],["\u003cp\u003eApps Script simplifies working with JSON responses through \u003ccode\u003eJSON.parse()\u003c/code\u003e for converting raw JSON into native objects and \u003ccode\u003eJSON.stringify()\u003c/code\u003e for the reverse.\u003c/p\u003e\n"],["\u003cp\u003eHandle XML responses from APIs using \u003ccode\u003eHTTPResponse.getContentText()\u003c/code\u003e and the \u003ccode\u003eXmlService\u003c/code\u003e for parsing and constructing XML data.\u003c/p\u003e\n"]]],[],null,["Google Apps Script can interact with APIs from all over the web. This guide\nshows how to work with different types of APIs in your scripts.\n\nConnect to public APIs\n\nYou can use the [`UrlFetch`](/apps-script/reference/url-fetch) service to make\nAPI requests directly.\n\nThe following example uses the\n[GitHub API](https://developer.github.com/v3/search/#search-repositories) to\nsearch for repositories with 100 or more stars that mention \"Apps Script\".\nThis API request does not require authorization or an API key. \n\n var query = '\"Apps Script\" stars:\"\u003e=100\"';\n var url = 'https://api.github.com/search/repositories'\n + '?sort=stars'\n + '&q=' + encodeURIComponent(query);\n\n var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});\n Logger.log(response);\n\nMake requests to services with OAuth\n\nAPIs that act on behalf of a user usually require authorization, often using the\n[OAuth protocol](http://oauth.net/). Apps Script doesn't provide built-in\nsupport for the protocol, but there are open source libraries you can use to\nperform the OAuth flow and send the credentials with your requests:\n\n- [OAuth1 for Apps Script](https://github.com/googlesamples/apps-script-oauth1): Compatible with OAuth 1.0 and 1.0a.\n- [OAuth2 for Apps Script](https://github.com/googlesamples/apps-script-oauth2): Compatible with OAuth2.\n\nWork with JSON\n\nWorking with JSON objects is similar to working with XML, except that parsing or\nencoding a JSON object is much easier.\n\nIf the API being requested returns a raw JSON response for a request, the JSON\nstring response can be accessed using the method\n[`HTTPResponse.getContentText()`](/apps-script/reference/url-fetch/http-response#getContentText()).\nOnce this string is retrieved, simply call `JSON.parse()` on the string to get a\nnative object representation. \n\n // Make request to API and get response before this point.\n var json = response.getContentText();\n var data = JSON.parse(json);\n Logger.log(data.title);\n\nLikewise, to make a string representation of a JavaScript object in order to\nmake a request, use `JSON.stringify()`. \n\n var data = {\n 'entry': {\n 'group': {\n 'title': 'Dog Skateboarding',\n 'description': 'My dog gets some serious air'\n },\n 'keywords': 'dog, skateboard'\n }\n }\n var payload = JSON.stringify(data);\n // Make request to API with payload after this point.\n\nParse XML\n\nIf an external API returns a raw XML response for a request, you can access the\nXML response using the method\n[`HTTPResponse.getContentText()`](/apps-script/reference/url-fetch/http-response#getContentText()). \n\n // Make request to API and get response before this point.\n var xml = response.getContentText();\n var doc = XmlService.parse(xml);\n\nWhen making XML requests to an API, construct the XML to send by using\nthe [`XmlService`](/apps-script/reference/xml-service/xml-service) methods. \n\n var root = XmlService.createElement('entry')\n .setAttribute('keywords', 'dog, skateboard');\n var group = XmlService.createElement('group')\n .setAttribute('title', 'Dog Skateboarding');\n .setAttribute('description', 'My dog gets some serious air');\n root.addContent(group);\n var document = XmlService.createDocument(root);\n var payload = XmlService.getPrettyFormat().format(document);\n // Make request to API with payload after this point."]]