yaxu
(Alex McLean)
9월 16, 2020, 5:57오후
1
모두 안녕하세요,
특정 이메일 주소를 기준으로 사용자를 한두 개의 그룹에 추가하고, 계정이 없으면 포럼 가입을 초대하는 API 기능을 찾고 있었습니다. 해당 기능을 찾을 수 없었지만, ‘일괄 업로드’ 초대 기능이 정확히 이 역할을 수행하는 것을 발견했습니다. 이 기능은 API 문서에 나와 있지 않지만, 브라우저에서는 이렇게 동작합니다.
그래서 저는 다음 PHP 코드를 사용하여 CSV 파일을 POST 요청으로 전송하는 동일한 방식을 시도했습니다:
$body = "--ossifrage\r
Content-Disposition: form-data; name=\"type\"\r
\r
csv\r
--ossifrage\r
Content-Disposition: form-data; name=\"files[]\"; filename=\"test.csv\"\r
Content-Type: text/csv\r
\r
$order_email,$group_names\r
\r
\r
--ossifrage--\r
";
$r=wp_remote_post( 'https://club.tidalcycles.org/invites/upload_csv.json', array(
'method' => 'POST',
'headers' => array('Content-Type' => 'multipart/form-data; boundary=ossifrage',
'Api-key' => '(redacted)',
'Api-Username' => 'yaxu'),
'body' => $body
)
);
이 방식은 몇 달 동안 잘 작동했지만, 최근에는 discourse 로그에 Can't verify CSRF token authenticity.라는 오류가 발생하면서 작동을 멈췄습니다.
API 키를 통해 인증되므로 이것이 버그인가요? 아니면 문서화되지 않은 API 부분을 사용하려는 시도가 실패로 끝나는 건가요?
yaxu
(Alex McLean)
9월 23, 2020, 9:27오후
2
혹시 이게 수정될 가능성이 있는 버그인지 확인해 주실 수 있을까요? 어쨌든 대안을 찾아보겠습니다.
이거에 대해 어떤 아이디어가 있나요 @techAPJ ?
yaxu
(Alex McLean)
9월 24, 2020, 8:23오전
4
안녕하세요, 확인해 주셔서 감사합니다. 문서화된 API를 사용하여 어설프게(hack) 무언가를 만들어 봤습니다(‘어설프게’라는 표현에 중점을 두지만, 다른 분들에게 유용할 수도 있을 것 같습니다):
function tidal_process_order( $order_id ){
$order = new WC_Order($order_id);
$order_email = $order->get_billing_email();
$group_names_arr = [];
// SKU를 그룹 이름으로 가져오기
foreach ( $order->get_items() as $item ) {
if ( $item['product_id'] > 0 ) {
$_product = $item->get_product();
$sku = $_product->get_sku();
array_push($group_names_arr, $sku);
}
}
$group_names = join(",",$group_names_arr);
// 이메일로 디스커스(Discourse) 사용자가 이미 존재하는지 확인
$r=wp_remote_get( "https://club.tidalcycles.org/admin/users/list/all.json?email=$order_email", array(
'headers' => array('Api-key' => 'redacted',
'Api-Username' => 'redacted')
)
);
$matches = json_decode($r['body']);
if (count($matches) > 0) {
// 사용자가 존재하므로 그룹에 추가
$user_id = $matches[0]->id;
foreach($group_names_arr as $group) {
// 그룹 이름에서 그룹 ID 가져오기
$r=wp_remote_get( "https://club.tidalcycles.org/groups/$group.json", array(
'headers' => array('Api-key' => 'redacted',
'Api-Username' => 'redacted')
)
);
$group = json_decode($r['body']);
$body = json_encode(array('group_id' => $group->group->id));
// 사용자를 그룹에 추가
$r=wp_remote_post( "https://club.tidalcycles.org/admin/users/$user_id/groups", array(
'headers' => array('Api-key' => 'redacted',
'Api-Username' => 'redacted',
'Content-Type' => 'application/json',
),
'body' => $body
));
}
}
else {
// 사용자가 존재하지 않으므로 그룹 이름과 함께 초대
$body = json_encode(array('email' => $order_email, 'group_names' => $group_names, "custom_message" => "Welcome!"));
$r=wp_remote_post( "https://club.tidalcycles.org/invites", array(
'headers' => array('Api-key' => 'redacted',
'Api-Username' => 'redacted',
'Content-Type' => 'application/json',
),
'body' => $body
));
};
}
add_action( 'woocommerce_order_status_processing', 'tidal_process_order' );