// GraalJS Script (engine type: 2)
//////// START "main()" /////////////////////////////////////////////////////////////////
main();
function main(){
//// == Config Retrieving / 工程コンフィグの参照 ==
const strAuthzSetting = configs.get ( "AuthzConfU" ); /// REQUIRED
engine.log( " AutomatedTask Config: Authz Setting: " + strAuthzSetting );
const filesPocketUploaded = configs.getObject( "SelectConfA" ); /// REQUIRED
let filesUploaded = engine.findData( filesPocketUploaded ); // java.util.ArrayList
if( filesUploaded === null ) {
throw new Error( "\n AutomatedTask UnexpectedFilesError:" +
" Files {A} not Attached \n" );
}else{
engine.log( " AutomatedTask FilesArray {A}: " +
filesUploaded.size() + " files" );
}
const strWpcomDomain = configs.get ( "StrConfB" ); /// REQUIRED
if( strWpcomDomain === "" ){
throw new Error( "\n AutomatedTask ConfigError:" +
" Config {B: WordPress.com domain} is empty \n" );
}
const strPocketTsv = configs.getObject( "SelectConfC" ); // NotRequired
//// == Data Retrieving / ワークフローデータの参照 ==
// (Nothing. Some workflow data is referenced via Expression Language in Config.)
//// == Calculating / 演算 ==
/// Upload Media
/// WordPress.com REST API
/// https://developer.wordpress.com/docs/api/
/// https://developer.wordpress.com/docs/api/1.1/post/sites/%24site/media/new/
// request1, prepare
let request1Uri = "https://public-api.wordpress.com/rest/v1.1/sites/" +
strWpcomDomain + "/media/new";
let request1 = httpClient.begin(); // HttpRequestWrapper
request1 = request1.authSetting( strAuthzSetting ); // with "Authorization: Bearer XX"
// https://questetra.zendesk.com/hc/en-us/articles/360024574471-R2300#HttpRequestWrapper
for( let i = 0; i < filesUploaded.size(); i++ ){
request1 = request1.multipart( ("media[" + i + "]"), filesUploaded[i] );
}
// request1, try
const response1 = request1.post( request1Uri ); // HttpResponseWrapper
engine.log( " AutomatedTask ApiRequest1 Start: " + request1Uri );
const response1Code = response1.getStatusCode() + "";
const response1Body = response1.getResponseAsString() + "";
engine.log( " AutomatedTask ApiResponse Status: " + response1Code );
if( response1Code !== "200"){
throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
response1Code + "\n" + response1Body + "\n" );
}
// response1, parse
/*
engine.log( response1Body ); // debug
{
"media":[{
"ID":3784,
"URL":"https:\/\/example.blog\/wp-content\/uploads\/2021\/06\/example.png",
"guid":"https:\/\/example.blog\/wp-content\/uploads\/2021\/06\/example.png",
"date":"2021-06-10T13:57:59+09:00",
"post_ID":0,
"author_ID":1,
"file":"example.png",
"mime_type":"image\/png",
"extension":"png",
"title":"example",
"caption":"",
"description":"",
"alt":"",
"icon":"https:\/\/example.blog\/wp-includes\/images\/media\/default.png",
"size":"40.62 KB",
"thumbnails":{ # # # },
"height":675,
"width":1200,
"exif":{ # # # },
"meta":{"links":{ # # # }}
},
{
"ID":3785,
# # #
}]
}
*/
const response1Obj = JSON.parse( response1Body );
let strTsv = "";
for( let i = 0; i < response1Obj.media.length; i++ ){
strTsv += response1Obj.media[i].ID;
strTsv += "\t";
strTsv += response1Obj.media[i].mime_type;
strTsv += "\t";
strTsv += response1Obj.media[i].URL;
if( i !== response1Obj.media.length - 1 ){
strTsv += "\n";
}
}
//// == Data Updating / ワークフローデータへの代入 ==
if( strPocketTsv !== null ){
engine.setData( strPocketTsv, strTsv );
}
} //////// END "main()" /////////////////////////////////////////////////////////////////
/*
Notes-en:
- Uploads the files to "Media" in your WordPress.com account.
- WordPress.com is a service that hosts WordPress sites, powered by Automattic itself.
- All files stored in the file type data will be uploaded automatically.
- Saves labor in website update workflow.
- Accepts jpg, jpeg, png, gif, pdf, doc, ppt, odt, pptx, docx, pps, ppsx, xls, xlsx, key.
- Audio and Video may also be available.
APPENDIX-en
- You can also refer to it by "Site ID" (blogid) instead of the WordPress.Com domain.
- Site ID (blogid) is obtained from `GET v1.1/sites/example.blog/` and the "View page source".
- Request is made to the "WordPress.com REST API" (WP.COM API).
- https://developer.wordpress.com/docs/api/
- Note: Not the "WP.REST API" for WordPress.Org.
- Setting example of "HTTP Authentication" (OAuth2)
- Authorization Endpoint URL:
- https://public-api.wordpress.com/oauth2/authorize
- Token Endpoint URL:
- https://public-api.wordpress.com/oauth2/token
- Scope:
- "global", "", "auth" or "media"
- (document) https://developer.wordpress.com/docs/oauth2/
- By default, the token will grant the application full access to a single blog.
- Needs a "global" scope, if to access to all the blogs that users have on WordPress.com.
- Client ID, Consumer Secret:
- ( from https://developer.wordpress.com/apps/ )
- Redirect URLs: https://s.questetra.net/oauth2callback
Notes-ja:
- WordPress.com アカウントの「メディア」にファイルをアップロードします。
- WordPress.com は WordPress sites をホストするサービスです。Automattic 社自身が提供しています。
- ファイル型データに格納されている全てのファイルが自動的にアップロードされます。
- ウェブサイト更新業務(ワークフロー)を省力化することができます。
- 対応ファイル jpg, jpeg, png, gif, pdf, doc, ppt, odt, pptx, docx, pps, ppsx, xls, xlsx, key
- Audio や Video も利用できる場合があります。
APPENDIX-ja
- WordPress.Com ドメインの代わりに「サイトID」(blogid)で参照することも可能です。
- サイトID(blogid)は、`GET v1.1/sites/example.blog/` や "ページのソース" 等から取得します。
- "WordPress.com REST API" (WP.COM API) に対してリクエストが投げられます。
- https://developer.wordpress.com/docs/api/
- 注: WordPress.Org 用の "WP.REST API" ではありません。
- "HTTP認証設定" の例 (OAuth2)
- 認可エンドポイント URL:
- https://public-api.wordpress.com/oauth2/authorize
- トークンエンドポイント URL:
- https://public-api.wordpress.com/oauth2/token
- スコープ:
- "global", "", "auth" or "media"
- (document) https://developer.wordpress.com/docs/oauth2/
- デフォルトでは、1つの blog に対するアクセスが認可されます。
- 全保有 blogs へアクセスできるようにするには "global" をセットします。
- クライアントID, クライアントシークレット:
- ( 開発者ページから取得してください⇒ https://developer.wordpress.com/apps/ )
- Redirect URLs: https://s.questetra.net/oauth2callback
*/
Pingback: Wordpress.Com: Media New – Questetra Support
Pingback: Wordpress.com: Posts, Create Draft – Questetra Support