LoginSignup
1
3

More than 5 years have passed since last update.

cloud9でMastodonのAPIを利用する

Posted at

最近Mastodonの話題が熱い。せっかくだし昔Twitterで動かしていたBOTをMastodonで動かしたく思い、調べてみた。
参考にさせていただいたサイトは下記。

まず使用するライブラリを落として解凍する

mkdir mastodon
cd mastodon
wget https://github.com/TheCodingCompany/MastodonOAuthPHP/archive/master.zip
unzip master.zip

ディレクトリの整理とかいらないファイルの削除とか。

cd MastodonOAuthPHP-master/
cp -r ./* ../
cd ../
rm -rf MastodonOAuthPHP-master
rm -rf master.zip

インスタンス名を定義する

Mastodon.php
  public function __construct($domainname = "インスタンスのドメイン") {        

        //Set the domain name to use
        $this->setMastodonDomain($domainname);
    }
oAuth.php
    /**
     * Our API to use
     * @var type 
     */
    private $mastodon_api_url = "インスタンスのドメイン";

    private $app_config = array(
        "client_name"   => "Mastodon", // ここにクライアント名を
        "redirect_uris" => "urn:ietf:wg:oauth:2.0:oob",
        "scopes"        => "read write",
        "website"       => "https://**********.c9users.io/" // ここに自分のCloud9のURLを
    );

index.phpを作成する

touch index.php

index.phpに下記を記述し、アプリを作成する

index.php
<?php
require_once("autoload.php");
$t = new \theCodingCompany\Mastodon();
$token_info = $t->createApp("MastodonAPI利用テスト", "https://**********.c9users.io"); // ここの第1引数が何からツイートしたかの表示部分になる
var_dump($token_info);

https://**********.c9users.io/mastodon へアクセスし、出てきたclient_idとclient_secretをoAuth.phpへコピペする

oAuth.php
    /**
     * Holds our client_id and secret
     * @var array 
     */
    private $credentials = array(
        "client_id"     => "****************************************************************", // client_id
        "client_secret" => "****************************************************************", // client_secret
        "bearer"        => ""
    );

認証してauthorization tokenを取得する(index.phpの中身は書き換える)

index.php
<?php
require_once("autoload.php");
$t = new \theCodingCompany\Mastodon();
$token_info = $t->createApp("MastodonAPI利用テスト", "https://**********.c9users.io"); // ここの第1引数が何からツイートしたかの表示部分になる
var_dump($token_info);

https://**********.c9users.io/mastodon へアクセスし、承諾後に出てくるキーをindex.phpへコピペする(index.phpの中身は書き換える)

index.php
<?php
require_once("autoload.php");
$t = new \theCodingCompany\Mastodon();

//Request the bearer token
$token_info = $t->getAccessToken("コピペした認証キー");
var_dump($token_info);

https://**********.c9users.io/mastodon へアクセスし、出てきたbearerをoAuth.phpへコピペする

oAuth.php
    /**
     * Holds our client_id and secret
     * @var array 
     */
    private $credentials = array(
        "client_id"     => "****************************************************************",
        "client_secret" => "****************************************************************",
        "bearer"        => "****************************************************************" // ここへコピーする
    );

登録確認を行う(index.phpの中身は書き換える)

index.php
<?php
require_once("autoload.php");
$t = new \theCodingCompany\Mastodon();

$bearer_token = $t->authenticate("Mastodonに登録したメアド", "Mastodonに登録したパスワード");
var_dump($bearer_token);

うまくいってればきちんとパラメータが表示され、Mastodonの設定→認証されたアプリのところに登録されているはず。

トゥート関数を作成する

Mastdon.php
/**
 * Posting a new status 仮
 */
public function PostStatuses($toot=null){

    if( $toot ){
        //Create our object
        $http = HttpRequest::Instance($this->getApiURL());
        $statusses = $http::Post(
            "api/v1/statuses",
            array(
                'status'    => $toot,
                'visibility'    => 'public'
            ),
            $this->getHeaders()
        );
        if(is_array($statusses) && count($statusses) > 0){
            return $statusses;
        }
    }
    return false;
}

トゥート用のファイルを作成する

touch toot.php
toot.php
<?php
require_once("autoload.php");
$t = new \theCodingCompany\Mastodon();
//Toot Test
$statusses = $t->PostStatuses("こんにちわ");
var_dump($statusses);

最後に
https://**********.c9users.io/mastodon/toot.php
へアクセスすれば、できているはずです。

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3