在IOS中使用app访问令牌

[英]Using app access token in IOS


Since Facebook changed their policy on having a access token or not I am now forced to include an access token to fetch a user public posts.

由于Facebook改变了他们关于拥有访问令牌的政策,我现在被迫包括访问令牌以获取用户公开帖子。

As read in the documentation I can use an app access token so I still can fetch the posts without asking a user for permissions. But since I have no clue how this works, and the facebook documentation doesn't give a example concerning how to implement this on IOS, I want to ask your help!

正如文档中所读,我可以使用app访问令牌,因此我仍然可以在不询问用户权限的情况下获取帖子。但由于我不知道这是如何工作的,并且facebook文档没有举例说明如何在IOS上实现这一点,我想请求你的帮助!

Does anyone has an example on how I need to implement this in my code? Thnx in advance!!

有没有人有一个关于如何在我的代码中实现这个的例子? Thnx提前!!

2 个解决方案

#1


10  

I'm not sure if it's what you are looking for, but if you are trying to do something simple like getting the public posts of a user, you can do it like this.

我不确定这是不是你想要的,但是如果你想做一些简单的事情,比如获取用户的公开帖子,你就可以这样做。

Use the following request to get the token:

使用以下请求获取令牌:

https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&client_secret=YOUR_APP_SECRET&grant_type=client_credentials

I'm guessing you've got your app id and secret since you've retrieved the token already. Anyway, the token will be returned in the response like this:

我猜你已经获得了你的app id和秘密,因为你已经检索了这个令牌。无论如何,令牌将在响应中返回,如下所示:

access_token=ACCESS_TOKEN

Parse out the token: and put it in the following request:

解析令牌:并将其放入以下请求中:

https://graph.facebook.com/FACEBOOK_USER_ID/posts?access_token=ACCESS_TOKEN

You can use the following code to parse out the token:

您可以使用以下代码解析令牌:

    NSRange access_token_range = [responseString rangeOfString:@"access_token="];
    if (access_token_range.length > 0) {
        int from_index = access_token_range.location + access_token_range.length;
        NSString *access_token = [responseString substringFromIndex:from_index];

        NSLog(@"access_token:  %@", access_token);
    }

EDIT: The get posts URL will give BadURL due to illegal chars. You can do like this to fix it:

编辑:由于非法字符,获取帖子URL将给出BadURL。你可以这样做来解决它:

NSString *encodedURLString = [yourUrlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];    
NSURL *url = [NSURL URLWithString:encodedURLString];

I'm not using the Facebook SDK, so I don't really know how it works, but I guess that you should do something like this:

我没有使用Facebook SDK,所以我真的不知道它是如何工作的,但我想你应该这样做:

[_facebook requestWithGraphPath:@"[user_id]/feed" 
                  andParams:[NSMutableDictionary dictionaryWithObject:ACCESS_TOKEN forKey:@"access_token"]
              andHttpMethod:@"GET"
                andDelegate:self];

#2


0  

Although you can do this, the API documentation states

尽管您可以这样做,但API文档说明了这一点

App Login allows you to take various administrative actions for your app, such as retrieving Insights data or approving Requests. Graph API calls that require an app access token are clearly denoted in the API reference."

应用程序登录允许您对应用程序执行各种管理操作,例如检索Insights数据或批准请求。需要app访问令牌的图形API调用在API参考中清楚地表示出来。“

I think it might be a security risk since someone could intercept your app access token and do all these things via the API. Not to mention having client_secret (which is basically a password for your app) being embedded in the iOS app. I think app logins are more for the PHP servers to use.

我认为这可能存在安全风险,因为有人可能会拦截您的应用访问令牌并通过API执行所有这些操作。更不用说在iOS应用程序中嵌入了client_secret(基本上是你的应用程序的密码)。我认为app登录更适合PHP服务器使用。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/06/05/75d05990c21c2578a905765266719e69.html



 
© 2014-2018 ITdaan.com 粤ICP备14056181号