J'ai essayé de frapper Mastdon pour le moment. Comme le titre l'indique, Ruby utilise Faraday, Python utilise Pycurl et PHP utilise Curl. J'ai enregistré l'application avec app.xx et utilisé test.xx pour m'authentifier et publier.
Ruby
app.rb
#!/usr/local/bin/ruby
# -*- coding: utf-8 -*-
require 'faraday'
require 'uri'
CALLBACK_URL = 'URL de l'application'
MASTODON_URL = 'URL de l'instance cible (par exemple: https://mstdn.high-low.ml)'
SCOPE = 'Autorisation que vous voulez (par exemple: read write)'
options = {
  client_name: 'Nom de l'application',
  redirect_uris: CALLBACK_URL,
  scopes: SCOPE
}
conn = Faraday.new(:url => MASTODON_URL) do |faraday|
  faraday.request :url_encoded
  faraday.response :logger
  faraday.adapter Faraday.default_adapter
end
response = conn.post do |request|
  request.url '/api/v1/apps'
  request.body = URI.encode_www_form(options)
end
print "Content-Type: application/json; charset=UTF-8\n\n"
print response.body
Enregistrez les "client_id" et "client_secret" affichés dans le résultat de sortie.
test.rb
#!/usr/local/bin/ruby
# -*- coding: utf-8 -*-
require 'cgi'
require 'uri'
cgi = CGI.new()
CALLBACK_URL = 'URL de l'application'
MASTODON_URL = 'URL de l'instance cible'
CLIENT_ID = 'app.CLIENT obtenu par rb_ID'
CLIENT_SECRET = 'app.CLIENT obtenu par rb_SECRET'
SCOPE = 'Autorité spécifiée'
if cgi['code'].empty?
  #Allez obtenir la certification
  options = {
    client_id: CLIENT_ID,
    response_type: 'code',
    scope: SCOPE,
    redirect_uri: CALLBACK_URL
  }
  print cgi.header({
    'status' => 'REDIRECT',
    'Location' => "#{MASTODON_URL}/oauth/authorize?#{URI.encode_www_form(options)}"
  })
else
  #Authentifié afin d'accéder_Je vais avoir un jeton
  require 'faraday'
  require 'json'
  options = {
    client_id: CLIENT_ID,
    client_secret: CLIENT_SECRET,
    grant_type: 'authorization_code',
    redirect_uri: CALLBACK_URL,
    scope: SCOPE,
    code: cgi['code']
  }
  conn = Faraday.new(:url => MASTODON_URL) do |faraday|
    faraday.request :url_encoded
    faraday.response :logger
    faraday.adapter Faraday.default_adapter
  end
  response = conn.post do |request|
    request.url '/oauth/token'
    request.body = URI.encode_www_form(options)
  end
  access_token = JSON.parse(response.body)['access_token']
  # access_Pendant que j'ai le jeton, je crie au centre du monde
  options = {
    mastodon_host: MASTODON_URL,
    status: 'Paon'
  }
  response = conn.post do |request|
    request.url '/api/v1/statuses'
    request.headers = {
      'Authorization' => "Bearer #{access_token}"
    }
    request.body = URI.encode_www_form(options)
  end
  print "Content-Type: application/json; charset=UTF-8\n\n"
  print response.body
end
L'écran d'authentification ressemble à ceci.

Python
app.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pycurl, urllib
from StringIO import StringIO
CALLBACK_URL = 'URL de l'application'
MASTODON_URL = 'URL de l'instance cible'
SCOPE = 'Autorisation que vous voulez (par exemple: read write)'
options = {
	'client_name': 'Nom de l'application',
	'redirect_uris': CALLBACK_URL,
	'scopes': SCOPE
}
response = StringIO()
curl = pycurl.Curl()
curl.setopt(pycurl.URL, '%s/api/v1/apps' %(MASTODON_URL))
curl.setopt(pycurl.WRITEFUNCTION, response.write)
curl.setopt(pycurl.POST, 1)
curl.setopt(pycurl.POSTFIELDS, urllib.urlencode(options))
curl.perform()
print "Content-Type: application/json; charset=UTF-8\n\n"
print response.getvalue()
test.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib, cgi
from StringIO import StringIO
form = cgi.FieldStorage()
CALLBACK_URL = 'URL de l'application'
MASTODON_URL = 'URL de l'instance cible'
CLIENT_ID = 'app.CLIENT obtenu par py_ID'
CLIENT_SECRET = 'app.CLIENT obtenu par py_SECRET'
SCOPE = 'Autorité spécifiée'
if not form.has_key('code'):
	#Je vais obtenir la certification
	options = {
		'client_id': CLIENT_ID,
		'response_type': 'code',
		'scope': SCOPE,
		'redirect_uri': CALLBACK_URL
	}
	print "Content-Type: text/html\n\n"
	print '<meta http-equiv=\"refresh\" content=\"0; URL=%s/oauth/authorize?%s\">' %(MASTODON_URL, urllib.urlencode(options))
else:
	# access_Je vais avoir un jeton
	import pycurl, json
	options = {
		'client_id': CLIENT_ID,
		'client_secret': CLIENT_SECRET,
		'grant_type': 'authorization_code',
		'redirect_uri': CALLBACK_URL,
		'scope': SCOPE,
		'code': form['code'].value
	}
	response = StringIO()
	curl = pycurl.Curl()
	curl.setopt(pycurl.URL, '%s/oauth/token' %(MASTODON_URL))
	curl.setopt(pycurl.WRITEFUNCTION, response.write)
	curl.setopt(pycurl.POST, 1)
	curl.setopt(pycurl.POSTFIELDS, urllib.urlencode(options))
	curl.perform()
	access_token = str(json.loads(response.getvalue())['access_token'])
	#Crier
	options = {
		'mastodon_host': MASTODON_URL,
		'status': 'Paon'
	}
	curl.setopt(pycurl.URL, '%s/api/v1/statuses' %(MASTODON_URL))
	curl.setopt(pycurl.HTTPHEADER, ['Authorization: Bearer %s' %(access_token)])
	curl.setopt(pycurl.POSTFIELDS, urllib.urlencode(options))
	curl.perform()
	print "Content-Type: application/json; charset=UTF-8\n\n"
	print response.getvalue()
PHP
app.php
<?php
define('CALLBACK_URL', 'URL de l'application');
define('MASTODON_URL', 'URL de l'instance cible (par exemple: https://mstdn.high-low.ml)');
define('SCOPE', 'Autorisation que vous voulez (par exemple: read write)');
$options = array(
	'client_name' => 'Nom de l'application',
	'redirect_uris' => CALLBACK_URL,
	'scopes' => SCOPE
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, MASTODON_URL. '/api/v1/apps');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $options);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
header('Content-type: application/json; charset=utf-8');
echo $response;
test.php
<?php
define('CALLBACK_URL', 'URL de l'application');
define('MASTODON_URL', 'URL de l'instance cible');
define('CLIENT_ID', 'app.CLIENT obtenu de php_ID');
define('CLIENT_SECRET', 'app.CLIENT obtenu de php_SECRET');
define('SCOPE', 'Autorité spécifiée');
if (!isset($_GET['code'])) {
	#Authentification
	$options = array(
		'client_id' => CLIENT_ID,
		'response_type' => 'code',
		'scope' => SCOPE,
		'redirect_uri' => CALLBACK_URL
	);
	header('Location: '. MASTODON_URL. '/oauth/authorize?'. http_build_query($options, '', '&'));
} else {
	# access_obtenir un jeton
	$options = array(
		'client_id' => CLIENT_ID,
		'client_secret' => CLIENT_SECRET,
		'grant_type' => 'authorization_code',
		'redirect_uri' => CALLBACK_URL,
		'scope' => SCOPE,
		'code' => $_GET['code']
	);
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, MASTODON_URL. '/oauth/token');
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $options);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$response = curl_exec($ch);
	$data = json_decode($response);
	$access_token = $data->access_token;
	#Paon
	$options = array(
		'mastodon_host' => MASTODON_URL,
		'status' => 'Paon'
	);
	curl_setopt($ch, CURLOPT_URL, MASTODON_URL. '/api/v1/statuses');
	curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer {$access_token}"));
	curl_setopt($ch, CURLOPT_POSTFIELDS, $options);
	$response = curl_exec($ch);
	curl_close($ch);
	header('Content-type: application/json; charset=utf-8');
	echo $response;
}
tootsuite/mastodon-api: A ruby interface for the Mastodon API Exemple d'utilisation de l'API de Mastodon avec curl --Qiita Comment accéder à l'API Mastodon
Recommended Posts