【问题标题】:Inviting people with Trello API使用 Trello API 邀请他人
【发布时间】:2016-12-06 12:59:51
【问题描述】:

我正在尝试通过电子邮件从我的网站邀请一个人加入 Trello。这是API reference。当我尝试邀请他时,简单的回答是“无效密钥”。这是我的功能:

public function inviteEmployeeToTrello ($email, $name, $isAdmin)
{
    $organazationTrelloID = 'myOrganazationID';
    $trelloAuthToken = 'myTrelloAuthToken';
    $trelloInviteUrl = 'https://trello.com/1/organizations/'.$organazationTrelloID.'/members';

    if ($isAdmin == 1)
    {
        $type = 'admin';
    }
    else
    {
        $type = 'normal';
    }

    $fields = array(
        'fullName' => $name,
        'email' => $email,
        'type' => $type,
        'token' => $trelloAuthToken
    );

    // open connection
    $ch = curl_init();

    // set the url, number of PUT vars, PUT data
    curl_setopt($ch, CURLOPT_URL, $trelloInviteUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

    // exec
    $replyRaw = curl_exec($ch);
    $reply = json_decode($replyRaw, true);

    // close connection
    curl_close($ch);

    dd($ch);
}

【问题讨论】:

    标签: php api curl trello


    【解决方案1】:

    正如错误代码所说,您忘记传递您的application key。这是来自API reference 的示例:

    https://api.trello.com/1/organizations/publicorg?members=all&member_fields=username,fullName&fields=name,desc&key=[application_key]&token=[optional_auth_token]

    您必须将其包含在您的查询中,因此在您的情况下,请将其添加到 $fields 数组:

    $fields = array(
        'fullName' => $name,
        'email' => $email,
        'type' => $type,,
        'key' => $trelloAppKey
        'token' => $trelloAuthToken
    );
    

    【讨论】:

    • 谢谢!我已将密钥添加到 $field 数组并用 http_build_query($fields) 替换了 json_encode($fields),现在它可以工作了!
    【解决方案2】:

    CURLOPT_POSTFIELDS 不需要 JSON, 如果你想要一个 urlencoded 请求,使用 http_build_query($fields) ,或者如果你想要一个 multipart/form-data 请求,直接给它 $fields 数组。 (不过,API 文档似乎没有提到它接受哪些请求类型。urlencoded 是最常见的类型。)

    【讨论】:

    • 谢谢!我已将密钥添加到 $field 数组并用 http_build_query($fields) 替换了 json_encode($fields),现在它可以工作了!
    猜你喜欢
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 2019-05-03
    • 2021-11-10
    • 1970-01-01
    • 2011-10-27
    • 2021-02-09
    • 1970-01-01
    相关资源
    最近更新 更多