c# - Discord Add Guild Member 401 Error Despite Apparently Valid Acces Token -


i new discord's api, , working project needs able add guild member programmatically. i've learned how authorization code (with identify , guilds.join scopes), redeem access token, , user's id. last step use access code , user id add guild. command detailed here:

https://discordapp.com/developers/docs/resources/guild#add-guild-member

it seems need send put request url:

https://discordapp.com/api/guilds/[guildid]/members/[userid]

but results in response:

{"code": 0, "message": "401: unauthorized"}

i have tried including access token in authorization header:

authorization: bearer [redacted]

i've tried adding json body request:

{"access_token":"[redacted]"}

neither has worked. unsurprisingly, using both @ same time hasn't worked either.

i wondered if permissions issue, discord confirms have guilds.join scope. json receive when exchanging authorization code access token:

{"access_token": "[redacted]", "token_type": "bearer", "expires_in": 604800, "refresh_token": "[redacted]", "scope": "identify guilds.join"}

the identify scope works since able retrieve user , id. guilds.join doesn't seem work.

i have included test code below. have marked "option 1" , "option 2" lines signify wouldn't typically both of these access code methods in same request. mentioned earlier, did try both, , still got 401 error.

using (webclient client = new webclient()) {     client.headers.add(httprequestheader.contenttype, "application/x-www-form-urlencoded");     client.headers.add(httprequestheader.authorization, "bearer [redacted]");//option 1     string output = client.uploadstring     (         "https://discordapp.com/api/guilds/[guildid]/members/[userid]",         webrequestmethods.http.put,         "{\"access_token\":\"[redacted]\"}"//option 2     ); } 

because i'd understand intracacies of how works, i'd prefer know how ordinary web requests (such httpwebrequest , webclient, opposed using oauth library).

i answering own question may have figured out. works when execute below:

using (webclient client = new webclient()) {     client.headers.add(httprequestheader.contenttype, "application/json");     client.headers.add(httprequestheader.authorization, "bot [redacted]");     string output = client.uploadstring     (         "https://discordapp.com/api/guilds/[guildid]/members/[userid]",         webrequestmethods.http.put,         "{\"access_token\":\"[redacted]\"}"     ); } 

notice changed content type of "application/x-www-form-urlencoded" "application/json." changed using authorization header of "bearer" "bot," , using bot's token. using same access token before. plan accept answer if no better solutions come in.


Comments

Popular posts from this blog

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

android - ConstraintLayout: Realign baseline constraint in case if dependent view visibility was set to GONE -

c# - Populating Gridview inside Listview ItemTemplate On Web User Control from Code Behind -