github.com/line/line-bot-sdk-go/v7@v7.21.0/examples/access_token_helper/main.go (about)

     1  // Copyright 2020 LINE Corporation
     2  //
     3  // LINE Corporation licenses this file to you under the Apache License,
     4  // version 2.0 (the "License"); you may not use this file except in compliance
     5  // with the License. You may obtain a copy of the License at:
     6  //
     7  //   http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    11  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    12  // License for the specific language governing permissions and limitations
    13  // under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"flag"
    19  	"log"
    20  	"os"
    21  
    22  	"github.com/line/line-bot-sdk-go/v7/linebot"
    23  )
    24  
    25  func main() {
    26  	var (
    27  		mode            = flag.String("mode", "list", "mode of access token v2 helper [issue|list|revoke]")
    28  		channelID       = flag.String("channel_id", os.Getenv("CHANNEL_ID"), "Channel ID on channel console")
    29  		accessToken     = flag.String("access_token", os.Getenv("ACCESS_TOKEN"), "channel access token")
    30  		clientAssertion = flag.String("client_assertion", os.Getenv("CLIENT_ASSERTION"), "A JSON Web Token the client needs to create and sign with the private key")
    31  	)
    32  	checkErr := func(err error) {
    33  		if err != nil {
    34  			log.Fatal(err)
    35  		}
    36  	}
    37  	flag.Parse()
    38  	bot, err := linebot.New(
    39  		os.Getenv("CHANNEL_SECRET"),
    40  		os.Getenv("CHANNEL_TOKEN"),
    41  	)
    42  	checkErr(err)
    43  
    44  	switch *mode {
    45  	case "issue":
    46  		res, err := bot.IssueAccessTokenV2(*clientAssertion).Do()
    47  		checkErr(err)
    48  		log.Printf("%v", res)
    49  	case "list":
    50  		res, err := bot.GetAccessTokensV2(*clientAssertion).Do()
    51  		checkErr(err)
    52  		log.Printf("%v", res)
    53  	case "revoke":
    54  		res, err := bot.RevokeAccessTokenV2(*channelID, os.Getenv("CHANNEL_SECRET"), *accessToken).Do()
    55  		checkErr(err)
    56  		log.Printf("%v", res)
    57  	default:
    58  		log.Fatal("implement me")
    59  	}
    60  }