oauth2.0 如何允许其他用户访问您的Streamlabs API应用程序?

b5buobof  于 2023-03-28  发布在  其他
关注(0)|答案(2)|浏览(144)

文档在这一点上不是很清楚,我能够在我的令牌中硬编码并让它像这样工作

const socketToken = 'mytokenrandomlongstringofchars';

//Connect to socket
const streamlabs = io(`https://sockets.streamlabs.com?token=${socketToken}`, {
      transports: ['websocket']
 });

然后监听事件本身

streamlabs.on("event", (eventData) => {
      if (eventData.for === "streamlabs" && eventData.type == "donation") {
        var donobj = eventData.message;
        var dononame = donobj[0].from;
        var donomessage = donobj[0].message;
        document.getElementById("alert-message").innerHTML = dononame;
        document.getElementById("alert-user-message").innerHTML = donomessage;
        console.log(donobj);
      }
    });

API使用oAuth 2来允许用户连接,这是我卡住的地方,文档中的片段并没有帮助,我应该将用户发送到类似于以下内容的授权链接

  • streamlabs.com/API/v1.0/authorize?client_id=CLIENT-ID-HERE&redirect_uri=REDIRECT-URI&response_type=code&scope=SOME+SCOPES+HERE*

根据this堆栈溢出应答器,一旦它们接受授权,它们就会被重定向到我在注册应用程序时指定的重定向URI,代码如下所示

  • my.site/callback?code=randomstringofcharacters*.

我应该用该代码向https://streamlabs.com/api/v1.0/token发送POST请求以获取访问令牌,然后使用该访问令牌向https://streamlabs.com/api/v1.0/socket/token发送GET请求,以便我最终可以获得可以用于侦听事件的Web Socket令牌,而我不知道如何从重定向的URL中获取代码,如果我正确地理解了这个过程,那么我应该能够管理其他步骤,但是我如何首先获得代码呢?

55ooxyrt

55ooxyrt1#

我不知道这是不是你想要的,但是你可以在streamlabs网站的应用程序设置下添加授权用户,如果需要澄清的话请告诉我。

q43xntqr

q43xntqr2#

对于捐赠警报,我创建了一个应用程序,工作正常。我在这里分享代码:-

protected void Page_Load(object sender, EventArgs e)
{

    if(Session["Resp"]!=null)
    {
        LBL2.Text=Session["Resp"].ToString();
        return;
    }
    int c=0;
    try
    {
        if (Session["Counter"] == null)
            Session.Add("Counter", 0);
        else{
            c = int.Parse(Session["Counter"].ToString()) + 1;
        }
        lbl1.Text = c.ToString();
        string CD = Request.QueryString["CODE"].ToString();
        code.Value = CD;
        form1.Method = "POST";
        //form1.Action = "https://streamlabs.com/api/v1.0/token";
        form1.Action = "https://streamlabs.com/api/v2.0/token";

        using (WebClient client = new WebClient())
        {
            ServicePointManager.Expect100Continue = true;

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                    | SecurityProtocolType.Ssl3;
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

            byte[] response =
            client.UploadValues("https://streamlabs.com/api/v2.0/token", new NameValueCollection()
           {
               { "client_id", client_id.Value },
               { "client_secret", client_secret.Value },
               { "redirect_uri", redirect_uri.Value },
               { "code", CD },
               { "grant_type", grant_type.Value },
           });

            string result = System.Text.Encoding.UTF8.GetString(response);
            token deserializedProduct = JsonConvert.DeserializeObject<token>(result);
            LBL2.Text = result;
            tok.Text = deserializedProduct.access_token;
        }

            
    }
    catch (Exception ee)
    {
    }
    //System.Web.HttpContext.Current.Response.End();  
}

将此文件保存为accessstoken.aspx,运行此代码将端口(在visual studio中)设置为1280,并将Streamlabs应用程序中的重定向URL设置为http://localhost:1280/accessstoken. aspx。然后在visual studio运行时从streamlabs应用程序运行TryIt链接。将接收AccessToken。然后我使用该访问令牌创建捐赠。请注意:您创建的StreamLabs应用程序需要得到他们的批准。
我使用的捐赠代码:

protected void Button1_Click(object sender, EventArgs e)
    {

        if (acctok == null || acctok.Length < 20)
        {
            Lbl1.Text = "No accesstoken";
            return;
        }

        using (WebClient client = new WebClient())
        {
            ServicePointManager.Expect100Continue = true;

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                    | SecurityProtocolType.Ssl3;
            ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;

            client.Headers.Add("Authorization", "Bearer " + acctok);
            client.Headers.Add("Content-Type", "application/json");
            client.Headers.Add("X-Requested-With", "XMLHttpRequest");
            //client.UseDefaultCredentials = true;
            string response = client.UploadString("https://streamlabs.com/api/v2.0/donations","POST", retJson2(txtName.Text, decimal.Parse(txtAmt.Text)));
            string result = response;   // System.Text.Encoding.UTF8.GetString(response);
            RetData2 deserializedProduct = JsonConvert.DeserializeObject<RetData2>(result);
            Lbl1.Text = deserializedProduct.donation_id;

        }
    }

希望能帮上忙。谢谢。

相关问题