0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C#でbase64エンコードしてBearerTokenを作成する

Posted at

背景

TwitterAPIのaccessTokenを取得するためのBearerTokenをC#で作成する必要があり、
調べたときの備忘録です。

コード

以下の流れで作成していきます。

  1. API keyとAPI secret keyを用意する
  2. API keyとAPI secret keyをコロンでつなぐ
  3. byteの配列に変換する
  4. base64に変換する
using System;

// API keyとAPI secret keyを用意する
string apiKey = "abcdefg";
string apiSecretKey = "kdsajfkllpwjioajfjdsa;fdksajf";

// API keyとAPI secret keyをコロンでつなぐ
string strBearerTokenCredentials = apiKey + ":" + apiSecretKey;

// byteの配列に変換する
byte[] byteBearerTokenCredentials = Encoding.ASCII.GetBytes(strBearerTokenCredentials);
Console.WriteLine(Encoding.ASCII.GetString(byteBearerTokenCredentials));

// stringのbase64に変換する
string strBearerTokenCredentialsBase64 = Convert.ToBase64String(byteBearerTokenCredentials);
Console.WriteLine(strBearerTokenCredentialsBase64);

結果

// base64エンコードする前
abcdefg:kdsajfkllpwjioajfjdsa;fdksajf

// base64エンコードした後
YWJjZGVmZzprZHNhamZrbGxwd2ppb2FqZmpkc2E7ZmRrc2FqZg==

上記の流れで作成していきましたが、無駄な処理であったり、もっと良い作り方がある場合はご教示いただけると幸いです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?