Ich habe vorerst versucht, Mastdon zu schlagen. Wie der Titel schon sagt, verwendet Ruby Faraday, Python Pycurl und PHP Curl. Ich habe die App bei app.xx registriert und test.xx zur Authentifizierung und Veröffentlichung verwendet.
Ruby
app.rb
#!/usr/local/bin/ruby
# -*- coding: utf-8 -*-
require 'faraday'
require 'uri'
CALLBACK_URL = 'App-URL'
MASTODON_URL = 'URL der Zielinstanz (z: https://mstdn.high-low.ml)'
SCOPE = 'Berechtigung, die Sie möchten (z: read write)'
options = {
client_name: 'App Name',
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
Speichern Sie die im Ausgabeergebnis angezeigten "client_id" und "client_secret".
test.rb
#!/usr/local/bin/ruby
# -*- coding: utf-8 -*-
require 'cgi'
require 'uri'
cgi = CGI.new()
CALLBACK_URL = 'App-URL'
MASTODON_URL = 'URL der Zielinstanz'
CLIENT_ID = 'app.KUNDE erhalten von rb_ID'
CLIENT_SECRET = 'app.KUNDE erhalten von rb_SECRET'
SCOPE = 'Spezifizierte Behörde'
if cgi['code'].empty?
#Holen Sie sich die Zertifizierung
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
#Authentifiziert also Zugriff_Ich werde einen Token bekommen
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_Während ich den Token bekam, schreie ich mitten in der Welt
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
Der Authentifizierungsbildschirm sieht folgendermaßen aus.
Python
app.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import pycurl, urllib
from StringIO import StringIO
CALLBACK_URL = 'App-URL'
MASTODON_URL = 'URL der Zielinstanz'
SCOPE = 'Berechtigung, die Sie möchten (z: read write)'
options = {
'client_name': 'App Name',
'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 = 'App-URL'
MASTODON_URL = 'URL der Zielinstanz'
CLIENT_ID = 'app.KUNDE von py erhalten_ID'
CLIENT_SECRET = 'app.KUNDE von py erhalten_SECRET'
SCOPE = 'Spezifizierte Behörde'
if not form.has_key('code'):
#Ich werde die Zertifizierung bekommen
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_Ich werde einen Token bekommen
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'])
#Schreien
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', 'App-URL');
define('MASTODON_URL', 'URL der Zielinstanz (z: https://mstdn.high-low.ml)');
define('SCOPE', 'Berechtigung, die Sie möchten (z: read write)');
$options = array(
'client_name' => 'App Name',
'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', 'App-URL');
define('MASTODON_URL', 'URL der Zielinstanz');
define('CLIENT_ID', 'app.KUNDE erhalten von php_ID');
define('CLIENT_SECRET', 'app.KUNDE erhalten von php_SECRET');
define('SCOPE', 'Spezifizierte Behörde');
if (!isset($_GET['code'])) {
#Authentifizierung
$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_bekomme Token
$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 Beispiel für das Schlagen der Mastodon-API mit Curl - Qiita So erreichen Sie die Mastodon-API
Recommended Posts