DX・デジタルトランスフォーメーション

AppStoreにて無料公開中! ”システム手帳”で検索! - AppStoreへ iPad版 - iPhone版

ブロックチェーン・暗号技術 | android | iOS | Web | アプリの著作権 | 日本のなりたち | 禅・大乗仏教 | 新しい社会

主要SNS(twitter、facebook、instagram)APIによるアプリ登録申請とトークン取得からタイムライン取得まで

SNSにログインしてユーザー基本情報を取得するだけなら問題ないのだが、第三者のSNS投稿タイムラインを取得したい場合には各SNSごとにスタンスが異なったりして統一されていない。

twitter以外は投稿者のログイン承認によってアクセス用トークンを取得する必要がある。

いづれの場合も認証にはOAuth2.0を使用する。

主要SNS(twitter、facebook、instagram)APIによるアプリ登録申請とトークン取得から投稿取得までをまとめておく。

<OAuth2.0 / HTTP通信方法>

OAuthを実現するにはLibraryやFrameworkが溢れているが、プロトコルを理解するためにも、簡単なので、オフィシャルのCocoa Framework、android Frameworkのみで実装することを勧める。サードパーティのフレームワークに頼るのはできるだけやめたほうがいい。

・iOS


    // URI定義
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:SNS_AUTH_URI] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.];

    // HTTP Method定義
    [urlRequest setHTTPMethod: @"POST"];

    // HTTP Header定義(Base64エンコードする)
    NSData *credential =[[NSString stringWithFormat:@"%@:%@",SNS_API_KEY, SNS_API_SECRET] dataUsingEncoding:NSUTF8StringEncoding];
    NSString *base64EncodedCredential = [credential base64EncodedStringWithOptions:0];    
    [urlRequest setValue:[NSString stringWithFormat:@"Basic %@", base64EncodedCredential] forHTTPHeaderField: @"Authorization"];
    [urlRequest setValue: @"application/x-www-form-urlencoded;charset=UTF-8" forHTTPHeaderField: @"Content-Type"];

    // HTTP Body定義(URLエンコードする)
   NSString *httpBody = [NSString stringWithFormat:@"%@=%@", [SNS_PARAM_NAME stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet], [SNS_PARAM_VALUE stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet alphanumericCharacterSet]];
    [urlRequest setHTTPBody: httpBody];
    
    // HTTP通信開始
    NSURLSession *urlSession = [NSURLSession sharedSession];
    [[urlSession dataTaskWithRequest: urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        // HTTP通信完了
    }] resume];

<twitter>

特徴

アプリケーション認証なので、twitterアプリに登録するだけでよい。審査はない。

アプリからtwitterにログインする必要がない。

twitter SDKは使用しない。

1)アプリ登録

https://dev.twitter.com

{API Key}と{API Secret}を取得する。

2)ベアラートークン(アプリケーション認証)を取得

  • URI

    
    https://api.twitter.com/oauth2/token
    

  • HTTP Method

    POST

  • HTTP Header

    
    Authorization:Basic {{API Key}:{API Secret}をbase64エンコード}
    Content-Type:application/x-www-form-urlencoded;charset=UTF-8
    

  • HTTP Body

    
    grant_type=client_credentials
    

  • HTTP Reply (Json)

    
    {
        "access_token" = "{Bearer Token}";
        "token_type" = bearer;
    }
    

3)特定ユーザのタイムライン取得

  • URI

    
    https://api.twitter.com/1.1/statuses/user_timeline.json
    

  • HTTP Method

    GET

  • HTTP Header

    Authorization: Bearer {2で取得したBearer Token}

  • GETパラメータ

    screen_name

    {twitter account}

    count

    {取得投稿数}

    exclude_replies

    true

    include_rts

    false

  • HTTP Reply (Json配列)

<instagram>

特徴

instagramにアプリ審査申請をする必要がある。(Permittion:basic)

審査は2日ほどかかる。

Sandboxモードではテスターアカウントのみでしかアクセスできない。

アプリからログイン認証する必要がある。

instagram SDKは使用しない。

1)アプリ登録

https://www.instagram.com/developer/

Security - Disable implicit OAuth:のチェックをはずす。

{Redirect URI}にはカスタムURLは登録できない。

アプリ使用動画を送信する必要がある。

{Client ID}を取得する。

2)アクセストークン取得要求を送信 (Client-Side (Implicit) Authentication)

  • URI

    
    https://api.instagram.com/oauth/authorize/
    

  • HTTP Method

    GET

  • GETパラメータ

    client_id

    {Client ID}

    redirect_uri

    {Redirect URI}

    response_type

    token

3)instagramログインページが表示される。

webViewで表示する。

ユーザ名、パスワードでログインする。

4)リダイレクトページのURLからアクセストークンを取得

webViewデリゲートメソッドによる。


http://redirect uri/#access_token={Access Token}

5)User IDを取得

  • URI

    
    https://api.instagram.com/v1/users/self/
    

  • HTTP Method

    GET

  • GETパラメータ

    access_token

    {4で取得したAccess Token}

6)ユーザータイムラインの取得

  • URI

    
    https://api.instagram.com/v1/users/{User ID}/media/recent/
    

  • HTTP Method

    GET

  • GETパラメータ

    count

    {取得投稿数}

    access_token

    {4で取得したAccess Token}

<facebook>

特徴

facebookにアプリ審査申請をする必要がある。(Permittion: user_post)

審査は2日ほどかかる。

developmentモードではログインしかできない。

アプリからログイン認証する必要がある。

ログイン認証のみfacebook SDKを使用する。

1)アプリ登録

https://developers.facebook.com

xcode app bundle identifierを登録する。

xcodeシミュレータアプリとアプリ使用動画を送信する必要がある。

{App ID}と{Secret}を取得するが、APIアクセス時には使用しない。

2)ユーザーアクセストークンを取得

facebook SDKを使用する。


iOSの場合
FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logInWithReadPermissions:@[@"user_posts"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
    if (error) {
    } else if (result.isCancelled) {
    } else {
        result.token.tokenString;	// User Access Token
        result.token.userID;	// User ID
    }
}];

3)タイムライン投稿(posts)を取得

  • URI

    
    https://graph.facebook.com/v2.7/{2で取得したUser ID}/posts
    

  • HTTP Method

    GET

  • GETパラメータ

    access_token

    {2で取得したUser Access Token}

2023年12月
     12
3456789
10111213141516
17181920212223
24252627282930
31      
android
iOS
web
アプリの著作権
ブロックチェーン/暗号技術
新しい社会
禅・大乗仏教
日本のなりたち

AppStoreにて無料公開中! ”システム手帳”で検索! - AppStoreへ iPad版 - iPhone版
デジタルトランスフォーメーションで新しい生活を提案!